Advertisement
Guest User

Graphics

a guest
Nov 30th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. point2d P0, P1, P2, P3, P4,P5;
  2. int radius = 5;
  3. int sensitive = 8;
  4. int dragged = -1;
  5. int activePoint = -1;
  6. point2d[] points;
  7.  
  8. class point2d {
  9.   float x, y;
  10.  
  11.   point2d(float x, float y) {
  12.     this.x = x;
  13.     this.y = y;
  14.   }
  15. }
  16.  
  17.  
  18. void setup(){
  19.   size(800,600);
  20.   points = new  point2d[6];
  21.   P0 = points [0] = new point2d(100,400);
  22.   P1 = points [1] = new point2d(150,250);  
  23.   P2 = points [2] = new point2d(270,200);  
  24.   P3 = points [3] = new point2d(400,250);  
  25.   P4 = points [4] = new point2d(450,350);  
  26.   P5 = points [5] = new point2d(470,400);  
  27.  
  28. }
  29.  
  30. int getActivePoint( point2d[] t, int size, int sens, int mousex, int mousey ) {
  31.   point2d M = new point2d( mousex, mousey );
  32.  
  33.    for ( int i = 0; i < size; i++ ) {
  34.         if ( pow(M.x-t[i].x, 2)+pow(M.y-t[i].y,2) < pow(sens, 2 ) )
  35.             return i;
  36.    }
  37.     return -1;
  38. }
  39.  
  40.  
  41. void draw(){
  42.  
  43.   stroke(255,0,0);
  44.   strokeWeight(10);
  45.   beginShape(POINTS);
  46.   vertex(P0.x, P0.y);
  47.   vertex(P1.x, P1.y);
  48.   vertex(P2.x, P2.y);
  49.   vertex(P3.x, P3.y);
  50.   vertex(P4.x, P4.y);
  51.   vertex(P5.x, P5.y);
  52.   endShape();
  53.  
  54.   strokeWeight(2);
  55.   noFill();
  56.   beginShape();
  57.   vertex(P0.x, P0.y);
  58.   vertex(P1.x, P1.y);
  59.   vertex(P2.x, P2.y);
  60.   vertex(P3.x, P3.y);
  61.   vertex(P4.x, P4.y);
  62.   vertex(P5.x, P5.y);
  63.   endShape();
  64.  
  65.   beginShape();
  66.  
  67.   for(float t=0 ; t<=1 ; t+= 0.01){
  68.     vertex( pow(1-t,5)*P0.x + 5*t*pow(1-t,4)*P1.x+ 10*t*t*pow(1-t,3)*P2.x+ 10*t*t*t*pow(1-t,2)*P3.x+ 5*t*t*t*t*pow(1-t,1)*P4.x + t*t*t*t*t*P5.x,
  69.             pow(1-t,5)*P0.y + 5*t*pow(1-t,4)*P1.y+ 10*t*t*pow(1-t,3)*P2.y+ 10*t*t*t*pow(1-t,2)*P3.y+ 5*t*t*t*t*pow(1-t,1)*P4.y + t*t*t*t*t*P5.y );
  70.   }
  71.  
  72.   endShape();
  73.  
  74. }
  75.  
  76. void mousePressed() {
  77.   activePoint = getActivePoint( points, 6, sensitive, mouseX, mouseY );
  78.   if (activePoint != -1 ) {  
  79.     dragged = activePoint;
  80.   }
  81. }
  82.  
  83. void mouseDragged() {
  84.   if(dragged!=-1) {
  85.     points[dragged].x = mouseX;
  86.     points[dragged].y = mouseY;    
  87.   }
  88. }
  89.  
  90. void mouseReleased() {
  91.   dragged = -1;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement