Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://math.stackexchange.com/questions/814950/how-can-i-rotate-a-coordinate-around-a-circle
- https://fr.wikipedia.org/wiki/Rotation_plane
- class Circle{
- constructor(centerX, centerY, rotationRadius, circleRadius) {
- this.rotationRadius = rotationRadius;
- this.r = circleRadius;
- this.center = new p5.Vector(centerX, centerY);
- this.pos = new p5.Vector(centerX, centerY + rotationRadius);
- }
- rotate(angleDegrees) {
- let angleRadians = angleDegrees * PI / 180;
- // this.pos.rotate(HALF_PI / 10);
- // x' = cos(θ)*(x−cx)−sin(θ)*(y−cy)+cx
- // y' = sin(θ)*(x−cx)+cos(θ)*(y−cy)+cy
- // this.pos.x = (this.pos.x - this.center.x) * cos(angleRadians) + (this.pos.y * sin(angleRadians);
- this.pos.x = (this.pos.x - this.center.x) * cos(angleRadians) - (this.pos.y - this.center.y) * sin(angleRadians) + this.center.x;
- // this.pos.y = -1 * this.pos.x * sin(angleRadians) + this.pos.y * cos(angleRadians);
- this.pos.y = (this.pos.x - this.center.x) * sin(angleRadians) + (this.pos.y - this.center.y) * cos(angleRadians) + this.center.y;
- }
- show() {
- fill(255);
- // console.log(this.pos.x, this.pos.y, this.r * 2);
- ellipse(this.pos.x, this.pos.y, this.r * 2);
- // ellipse(100, 150, 20);
- }
- }
- var cir;
- function setup() {
- createCanvas(600,600);
- cir = new Circle(100, 100, 50, 10);
- // frameRate(2);
- }
- function draw() {
- // translate(width / 2, height / 2);
- background(0);
- cir.rotate(10);
- cir.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement