Advertisement
yclee126

Midpoint perpendicular points (Processing 3)

Jul 4th, 2022
1,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. // midpoint perpendicular points
  2. // drag mouse left or right to move points
  3. // move wheel to adjust the distance
  4.  
  5. // Code for Processing 3
  6.  
  7. float x1 = 0, y1 = 0;
  8. float x2 = 0, y2 = 0;
  9.  
  10. float newLen = 30;
  11.  
  12. void setup(){
  13.   size(500, 500);
  14.   fill(0, 0);
  15. }
  16.  
  17. void draw(){
  18.   background(255);
  19.  
  20.   line(x1, y1, x2, y2);
  21.   circle(x1, y1, 12);
  22.   circle(x2, y2, 12);
  23.  
  24.   float cx = (x1+x2)/2;
  25.   float cy = (y1+y2)/2;
  26.  
  27.   float dx = x1 - cx;
  28.   float dy = y1 - cy;
  29.   float len = sqrt(dx*dx + dy*dy);
  30.  
  31.   float fx1 = dy/len*newLen + cx;
  32.   float fy1 = -dx/len*newLen + cy;
  33.   circle(fx1, fy1, 20);
  34.  
  35.   float fx2 = -dy/len*newLen + cx;
  36.   float fy2 = dx/len*newLen + cy;
  37.   circle(fx2, fy2, 20);
  38.  
  39.   line(fx1, fy1, fx2, fy2);
  40. }
  41.  
  42. void mouseDragged() {
  43.   if (mouseButton == LEFT) {
  44.     x1 = mouseX;
  45.     y1 = mouseY;
  46.   } else if (mouseButton == RIGHT) {
  47.     x2 = mouseX;
  48.     y2 = mouseY;
  49.   }
  50. }
  51.  
  52. void mouseWheel(MouseEvent event) {
  53.   newLen += event.getCount()*5;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement