/* * Harriet Fell, Northeastern University, August 2006 */ import java.awt.*; import java.awt.geom.Point2D; import java.lang.Math; public class bezier extends BufferedApplet // based of Perlin's bspline { int w = 1, h; Color lightGreen = new Color(160,255,160); // Default Arch Points Point2D.Double P = new Point2D.Double(.125, .5); Point2D.Double Q = new Point2D.Double(.375, .25); Point2D.Double R = new Point2D.Double(.675, .25); Point2D.Double S = new Point2D.Double(.875, .5); int dtSz = 7; // dot size boolean newP = false, newQ = false, newR = false, newS = false; public void render(Graphics g) { w = bounds().width; // FETCH APPLET WIDTH,HT h = bounds().height; g.setColor(Color.white); // FILL BACKGROUND g.fillRect(0,0,w,h); // Scale to Window Size Point2D.Double Pd = new Point2D.Double(w*P.getX(), h*P.getY()); Point2D.Double Qd = new Point2D.Double(w*Q.getX(), h*Q.getY()); Point2D.Double Rd = new Point2D.Double(w*R.getX(), h*R.getY()); Point2D.Double Sd = new Point2D.Double(w*S.getX(), h*S.getY()); showArch(g, Pd, Qd, Rd, Sd); } public void showArch(Graphics g, Point2D.Double P, Point2D.Double Q, Point2D.Double R, Point2D.Double S){ // Draw Arch Lines g.setColor(lightGreen); g.drawLine((int)P.getX(), (int)P.getY(), (int)Q.getX(), (int)Q.getY()); g.drawLine((int)Q.getX(), (int)Q.getY(), (int)R.getX(), (int)R.getY()); g.drawLine((int)R.getX(), (int)R.getY(), (int)S.getX(), (int)S.getY()); // Draw Bezier Curve g.setColor(Color.black); drawArch(g, P, Q, R, S); // Draw Arch Points g.setColor(Color.red); g.fillOval((int)P.getX()-dtSz,(int)P.getY()-dtSz,2*dtSz,2*dtSz); g.fillOval((int)Q.getX()-dtSz,(int)Q.getY()-dtSz,2*dtSz,2*dtSz); g.fillOval((int)R.getX()-dtSz,(int)R.getY()-dtSz,2*dtSz,2*dtSz); g.fillOval((int)S.getX()-dtSz,(int)S.getY()-dtSz,2*dtSz,2*dtSz); } public void drawArch(Graphics g, Point2D.Double P, Point2D.Double Q, Point2D.Double R, Point2D.Double S){ if (ArchSize(P, Q, R, S) <= .5 ) g.drawLine((int) P.getX(), (int) P.getY(), (int) S.getX(), (int) S.getY()); else{ Point2D.Double PQ, QR, RS, PQR, QRS, PQRS; PQ = new Point2D.Double((P.getX() + Q.getX())/2, (P.getY() + Q.getY())/2); QR = new Point2D.Double((Q.getX() + R.getX())/2, (Q.getY() + R.getY())/2); RS = new Point2D.Double((R.getX() + S.getX())/2, (R.getY() + S.getY())/2); PQR = new Point2D.Double((PQ.getX() + QR.getX())/2, (PQ.getY() + QR.getY())/2); QRS = new Point2D.Double((QR.getX() + RS.getX())/2, (QR.getY() + RS.getY())/2); PQRS = new Point2D.Double((PQR.getX() + QRS.getX())/2, (PQR.getY() + QRS.getY())/2); drawArch(g,P, PQ, PQR, PQRS); drawArch(g, PQRS, QRS, RS, S); } } double ArchSize(Point2D.Double P, Point2D.Double Q, Point2D.Double R, Point2D.Double S){ double xmin = P.getX(), ymin = P.getY(); double xmax = P.getX(), ymax = P.getY(); if (Q.getX() < xmin) xmin = Q.getX(); else if (Q.getX() > xmax) xmax = Q.getX(); if (Q.getY() < ymin) ymin = Q.getY(); else if (Q.getY() > ymax) ymax = Q.getY(); if (R.getX() < xmin) xmin = Q.getX(); else if (R.getX() > xmax) xmax = R.getX(); if (R.getY() < ymin) ymin = R.getY(); else if (R.getY() > ymax) ymax = R.getY(); if (S.getX() < xmin) xmin = S.getX(); else if (S.getX() > xmax) xmax = S.getX(); if (S.getY() < ymin) ymin = S.getY(); else if (S.getY() > ymax) ymax = S.getY(); if (xmax - xmin > ymax - ymin) return (xmax - xmin); else return (ymax - ymin); } int mode; public boolean keyUp(Event e, int key) { mode = key; damage = true; return true; } public boolean mouseDown(Event e, int x, int y) { //System.out.println(P.toString()); //System.out.println((double)x/w + " " + (double)y/h); if ((w*P.getX()-x)*(w*P.getX()-x) + (h*P.getY()-y)*(h*P.getY()-y) <= dtSz*dtSz) { newP = true; newQ = false; newR = false; newS = false; } else if ((w*Q.getX()-x)*(w*Q.getX()-x) + (h*Q.getY()-y)*(h*Q.getY()-y) <= dtSz*dtSz) { newQ = true; newP = false; newR = false; newS = false; } else if ((w*R.getX()-x)*(w*R.getX()-x) + (h*R.getY()-y)*(h*R.getY()-y) <= dtSz*dtSz) { newR = true; newP = false; newQ = false; newS = false; } else if ((w*S.getX()-x)*(w*S.getX()-x) + (h*S.getY()-y)*(h*S.getY()-y) <= dtSz*dtSz) { newS = true; newP = false; newQ = false; newR = false; } else { newP = false; newQ = false; newR = false; newS = false; } return true; } public boolean mouseDrag(Event e, int x, int y) { if (newP) { P = new Point2D.Double((double)x/w,(double)y/h); } else if (newQ) { Q = new Point2D.Double((double)x/w,(double)y/h); } else if (newR) { R = new Point2D.Double((double)x/w,(double)y/h); } else if (newS) { S = new Point2D.Double((double)x/w,(double)y/h); } damage = true; return true; } }