import java.awt.*; public class segment extends line { public segment(point P, point Q) { super(P, Q); } public segment(double x1, double y1, double x2, double y2) { super(x1, y1, x2, y2); } public segment(point P, double m) { super(P, m); } public double length() { return(Math.sqrt((p.x - q.x)*(p.x - q.x) + (p.y - q.y)*(p.y - q.y))); } public void draw(GeometryWindow G) { if (!exists() || hidden) return; G.setColor(color); G.drawSegment(p.x, p.y, q.x, q.y); } public point coordOnShape(double X, double Y) { point P = super.coordOnShape(X, Y); double tx = q.x - p.x; double ty = q.y - p.y; double pTanDist = p.x * tx + p.y * ty; double qTanDist = q.x * tx + q.y * ty; double tanDist = P.x*tx + P.y*ty; if (tanDist < pTanDist) return(new point(p.x, p.y)); else if (tanDist > qTanDist) return(new point(q.x, q.y)); else return(P); } public boolean mouseDown(double X, double Y) { if (hidden) return(false); double m = slope(); double tx = q.x - p.x; double ty = q.y - p.y; double nx = -(q.y - p.y); double ny = q.x - p.x; double length = Math.sqrt(tx*tx + ty*ty); tx /= length; ty /= length; length = Math.sqrt(nx*nx + ny*ny); nx /= length; ny /= length; double baseDist = p.x * nx + p.y * ny; double pointDist = X * nx + Y * ny; double pTanDist = p.x * tx + p.y * ty; double qTanDist = q.x * tx + q.y * ty; double tanDist = X * tx + Y * ty; if ( (Math.abs(pointDist - baseDist) < clickRange) && (tanDist < qTanDist) && (tanDist > pTanDist) ) { dragging = true; double dx = (pointDist - baseDist) * nx; double dy = (pointDist - baseDist) * ny; p.translate(dx, dy); q.translate(dx, dy); dragX = X; dragY = Y; return(true); } return(false); } };