oscillator

This commit is contained in:
gauthiier 2015-03-03 15:20:55 +01:00
parent 31dfd2ab1d
commit 32d1805791
7 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,21 @@
class Circle extends Transformable {
int _radius;
public Circle(int x, int y, int r) {
super(x, y);
_radius = r;
ellipseMode(RADIUS);
}
public void draw_shape(){
ellipse(0, 0, _radius, _radius);
line(0, 0, _radius, 0);
}
public boolean inside(int mx, int my) {
if(dist(mx, my, _position.x, _position.y) < _radius) return true;
return false;
}
};

View File

@ -0,0 +1,19 @@
class Oscillator {
float _step, _theta;
float _min, _max, _length;
public Oscillator(float min, float max, float step) {
_min = min;
_max = max;
_length = max - min;
_step = step;
_theta = 0;
}
public float update() {
_theta += _step;
return (sin(_theta) * _length / 2) + _length / 2;
}
}

View File

@ -0,0 +1,22 @@
class Square extends Transformable {
int _width;
int _height;
public Square(int x, int y, int w, int h) {
super(x, y);
_width = w;
_height = h;
rectMode(CENTER);
}
public void draw_shape(){
rect(0, 0, _width, _height);
}
public boolean inside(int mx, int my) {
if(dist(mx, my, _position.x, _position.y) < _width /2) return true;
return false;
}
};

View File

@ -0,0 +1,86 @@
class Transformable {
// attaributes
PVector _position;
float _rotation;
float _scale;
// constructor (without arguments)
public Transformable() {
_position = new PVector(0, 0);
_rotation = 0;
_scale = 1.0;
}
// constructor (with arguments)
public Transformable(int x, int y) {
_position = new PVector(x, y);
_rotation = 0;
_scale = 1.0;
}
/////////////////////////////////
// (absolute) transforms
public void rotate_to(float r) {
_rotation = r;
}
public void translate_to(int x, int y) {
_position.x = x;
_position.y = y;
}
public void scale_to(float s) {
_scale = s;
}
///////////////////////////////////
// (incremental) transforms
public void rotate_increment(float r) {
_rotation += r;
}
public void translate_increment(int x, int y) {
_position.x += x;
_position.y += y;
}
public void scale_increment(float s) {
_scale += s;
}
///////////////////////////////////
// display method
// + applies transformations
// + draws shape of the subclass
public void display() {
pushMatrix();
translate(_position.x, _position.y);
rotate(_rotation);
scale(_scale);
draw_shape();
popMatrix();
}
///////////////////////////////////
// helper method - distance_from
// + computes distance from other position
public float distance_from(int x, int y) {
return dist(_position.x, _position.y, x, y);
}
///////////////////////////////////
// ** METHODS FOR SUBCLASSES **
// draws a (subclass) shape (in own coordinates)
public void draw_shape(){}
// evaluates if mx and my are inside the (subclass) shape
public boolean inside(int mx, int my){return false;}
};

View File

@ -0,0 +1,37 @@
class Triangle extends Transformable {
int _radius;
PVector A, B, C;
public Triangle(int x, int y, int r) {
super(x, y);
_radius = r;
A = new PVector();
B = new PVector();
C = new PVector();
float theta = TWO_PI/3;
A.x = cos(0 * theta) * _radius;
A.y = sin(0 * theta) * _radius;
B.x = cos(1 * theta) * _radius;
B.y = sin(1 * theta) * _radius;
C.x = cos(2 * theta) * _radius;
C.y = sin(2 * theta) * _radius;
}
public void draw_shape(){
triangle(A.x, A.y, B.x, B.y, C.x, C.y);
line(0, 0, A.x, 0);
}
public boolean inside(int mx, int my) {
if(dist(mx, my, _position.x, _position.y) < _radius) return true;
return false;
}
};

View File

@ -0,0 +1,2 @@
mode.id=processing.mode.java.JavaMode
mode=Java

View File

@ -0,0 +1,29 @@
final int CANVAS_WIDTH = 500;
final int CANVAS_HEIGHT = 500;
final int NBR_ELEMENTS = 16;
Transformable t;
Oscillator osc = new Oscillator(0, 5, 0.01);
void setup() {
size(CANVAS_WIDTH, CANVAS_HEIGHT);
background(23, 68, 250);
t = new Triangle(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2, 50);
}
void draw() {
background(23, 68, 250);
t.rotate_increment(0.01);
t.scale_to(osc.update());
stroke(255);
noFill();
t.display();
}