sketch_transformabel_click_create
This commit is contained in:
parent
851989a292
commit
c1f2e00c38
21
day2/sketch_transformable_click_create/Circle.pde
Normal file
21
day2/sketch_transformable_click_create/Circle.pde
Normal 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;
|
||||
}
|
||||
|
||||
};
|
||||
22
day2/sketch_transformable_click_create/Square.pde
Normal file
22
day2/sketch_transformable_click_create/Square.pde
Normal 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;
|
||||
}
|
||||
|
||||
};
|
||||
86
day2/sketch_transformable_click_create/Transformable.pde
Normal file
86
day2/sketch_transformable_click_create/Transformable.pde
Normal 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;}
|
||||
|
||||
};
|
||||
37
day2/sketch_transformable_click_create/Triangle.pde
Normal file
37
day2/sketch_transformable_click_create/Triangle.pde
Normal 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;
|
||||
}
|
||||
|
||||
};
|
||||
2
day2/sketch_transformable_click_create/sketch.properties
Normal file
2
day2/sketch_transformable_click_create/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
||||
mode.id=processing.mode.java.JavaMode
|
||||
mode=Java
|
||||
@ -0,0 +1,75 @@
|
||||
final int CANVAS_WIDTH = 500;
|
||||
final int CANVAS_HEIGHT = 500;
|
||||
|
||||
ArrayList<Transformable> elements = new ArrayList<Transformable>();
|
||||
|
||||
void setup() {
|
||||
size(CANVAS_WIDTH, CANVAS_HEIGHT, OPENGL);
|
||||
smooth();
|
||||
background(23, 68, 250);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
background(23, 68, 250);
|
||||
|
||||
if(frameCount % 2 == 0) {
|
||||
|
||||
for(int i = 0; i < elements.size(); i++) {
|
||||
Transformable t = elements.get(i);
|
||||
t.rotate_increment(0.01);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
stroke(255);
|
||||
noFill();
|
||||
|
||||
for(int i = 0; i < elements.size(); i++) {
|
||||
Transformable t = elements.get(i);
|
||||
t.display();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
|
||||
float r = random(0, 3);
|
||||
Transformable t;
|
||||
|
||||
if(r < 1) {
|
||||
t = new Circle(mouseX, mouseY, 25);
|
||||
} else if(r < 2) {
|
||||
t = new Square(mouseX, mouseY, 35, 35);
|
||||
} else {
|
||||
t = new Triangle(mouseX, mouseY, 25);
|
||||
}
|
||||
|
||||
elements.add(t);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void mouseDragged() {
|
||||
|
||||
for(int i = 0; i < elements.size(); i++) {
|
||||
Transformable t = elements.get(i);
|
||||
if(t.inside(mouseX, mouseY)) {
|
||||
t.translate_to(mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user