Transformable sketch
This commit is contained in:
parent
e317afc96e
commit
851989a292
21
day2/sketch_transformable/Circle.pde
Normal file
21
day2/sketch_transformable/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/Square.pde
Normal file
22
day2/sketch_transformable/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/Transformable.pde
Normal file
86
day2/sketch_transformable/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/Triangle.pde
Normal file
37
day2/sketch_transformable/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/sketch.properties
Normal file
2
day2/sketch_transformable/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mode.id=processing.mode.java.JavaMode
|
||||||
|
mode=Java
|
||||||
15
day2/sketch_transformable/sketch_transformable.pde
Normal file
15
day2/sketch_transformable/sketch_transformable.pde
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
final int CANVAS_WIDTH = 500;
|
||||||
|
final int CANVAS_HEIGHT = 500;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
size(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||||
|
background(23, 68, 250);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw() {
|
||||||
|
background(23, 68, 250);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mousePressed() {
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user