You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.2 KiB
Java

import processing.core.*;
import static processing.core.PApplet.*;
public class Rotate {
float centerx, centery, radius, speed;
int timeElapsed, millisInit;
PImage rotateRegion = null;
private boolean centercomplete;
Rotate(float speed) {
this.speed = speed;
this.centercomplete = false;
}
Rotate(float centerx, float centery, float radius, float speed) {
this.centerx = centerx;
this.centery = centery;
this.radius = radius;
this.speed = speed;
this.centercomplete = true;
}
void saveRotateRegion(ProjectorApplet p) {
this.rotateRegion = p.get(
(int) p.convertXCoord(centerx - radius),
(int) p.convertYCoord(centery - radius),
(int) p.convertXDistance(2*radius),
(int) p.convertYDistance(2*radius));
}
ProjectorCommand makeProjectorCommand() {
return (ProjectorApplet p) -> {
if (rotateRegion == null) {
millisInit = p.millis();
saveRotateRegion(p);
}
timeElapsed = p.millis() - millisInit;
p.noFill();
p.noStroke();
p.rectMode(CENTER);
p.rect(p.convertXCoord(centerx), p.convertYCoord(centery),
p.convertXDistance(radius), p.convertYDistance(radius));
p.pushMatrix();
p.translate(p.convertXCoord(centerx), p.convertYCoord(centery));
p.rotate(radians(speed * timeElapsed/1000f));
p.imageMode(CENTER);
p.image(rotateRegion, 0, 0, p.convertXDistance(2*radius), p.convertYDistance(2*radius));
p.popMatrix();
return false;
};
}
ClickGetter makeClickGetter(ProjectorApplet p){
return (int x, int y) -> {
if (!centercomplete){
centerx = x;
centery = y;
centercomplete = true;
return false;
} else {
radius = sqrt(pow(x - centerx, 2) + pow(y - centery, 2));
p.commandQueue.add(makeProjectorCommand());
return true;
}
};
}
}