Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. https://math.stackexchange.com/questions/814950/how-can-i-rotate-a-coordinate-around-a-circle
  2.  
  3. https://fr.wikipedia.org/wiki/Rotation_plane
  4.  
  5. class Circle{
  6. constructor(centerX, centerY, rotationRadius, circleRadius) {
  7. this.rotationRadius = rotationRadius;
  8. this.r = circleRadius;
  9. this.center = new p5.Vector(centerX, centerY);
  10. this.pos = new p5.Vector(centerX, centerY + rotationRadius);
  11. }
  12. rotate(angleDegrees) {
  13. let angleRadians = angleDegrees * PI / 180;
  14. // this.pos.rotate(HALF_PI / 10);
  15. // x' = cos(θ)*(x−cx)−sin(θ)*(y−cy)+cx
  16. // y' = sin(θ)*(x−cx)+cos(θ)*(y−cy)+cy
  17.  
  18. // this.pos.x = (this.pos.x - this.center.x) * cos(angleRadians) + (this.pos.y * sin(angleRadians);
  19. this.pos.x = (this.pos.x - this.center.x) * cos(angleRadians) - (this.pos.y - this.center.y) * sin(angleRadians) + this.center.x;
  20. // this.pos.y = -1 * this.pos.x * sin(angleRadians) + this.pos.y * cos(angleRadians);
  21. this.pos.y = (this.pos.x - this.center.x) * sin(angleRadians) + (this.pos.y - this.center.y) * cos(angleRadians) + this.center.y;
  22.  
  23. }
  24. show() {
  25. fill(255);
  26. // console.log(this.pos.x, this.pos.y, this.r * 2);
  27. ellipse(this.pos.x, this.pos.y, this.r * 2);
  28. // ellipse(100, 150, 20);
  29. }
  30. }
  31.  
  32. var cir;
  33. function setup() {
  34. createCanvas(600,600);
  35. cir = new Circle(100, 100, 50, 10);
  36. // frameRate(2);
  37. }
  38.  
  39. function draw() {
  40. // translate(width / 2, height / 2);
  41. background(0);
  42. cir.rotate(10);
  43. cir.show();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement