/***************************************************************** * File: Line2d.java * Synopsis: * Basic 2-dimensional geometry * $Id: Line2d.j,v 1.1 2003/02/12 17:57:36 yap Exp $ *****************************************************************/ class Line2d extends GeomObj { /* An instance l of the data type $line$ is a directed straight line in the two dimensional plane. The angle between a right oriented horizontal line and $l$ is called the direction of $l$. */ /* member Vector is not used in this class, it's intended for use in the operator+,- etc need to do: assure p0 != p1 */ public: Point2d p0; Point2d p1; Point2d V; // vector direction double a, b, c; // equation /************************************************************* * constructors *************************************************************/ Line2d(Point2d p, Vector v); // line initialized to pass through points p and p+v Line2d(Point2d p, Point2d q); //line is initialized to pass through points p and q directed from p to q // Line2d(point p, double alpha); //line passes through point p with direction alpha Line2d(Line2d ); Line2d(); //line passes through the origin with direction 0. virtual ~Line2d() {} /************************************************************* * member functions *************************************************************/ Vector direction() { return p1-p0; } // returns the direction as a vector Point2d startPt() { return p0; } Point2d stopPt() { return p1; } double distance(Point2d q) ; // returns the Euclidean distance between this line and point q Point2d projection(Point2d p) ; // returns the projection of p on this line int orientation( Point2d p ) ; // orientation of p0, p1 and p // the sine/cosine of the angle made with positive x-direction double sine() { return (p1.Y() - p0.Y()) / p0.distance(p1); } double cosine() { return (p1.X() - p0.X()) / p0.distance(p1); } Line2d rotate90( Point2d q) { return Line2d(startPt().rotate90(q), stopPt().rotate90(q)); } double y_abs() ; // returns the y-abscissa of the line double slope() ; //precond: is not vertical /************************************************************* * predicates *************************************************************/ bool isVertical() { return p0.X() == p1.X(); } bool isHorizontal() { return p0.Y() == p1.Y(); } bool isTrivial() {return p0 == p1; } //meaning for a line? bool contains( Point2d p) { return orientation2d(p0, p1, p) == 0; } bool isCoincident( Line2d g) { return contains(g.p0) contains(g.p1); } bool isParallel(Line2d l) { return det(V, l.direction()) == 0; } bool isEqual( Line2d g ) { return isCoincident(g); } /************************************************************* * intersection *************************************************************/ int intersects(Line2d t) ; // decides whether *this and t intersects // return dim of intersection. // return -1 if no intersection GeomObj* intersection(Line2d g) ; //if this line and g intersect in a single point, this point is // assigned to p and the result is true, otherwise the result is false /************************************************************* * angles and others *************************************************************/ int orientation2d( Line2d l, Point2d p); // computes the orientation (a, b, p), where a!=b and a and b appear // in this order on line l int cmp_slopes(Line2d l1, Line2d l2) //l1.slope > l2.slope: +1; equal: 0; otherwise: -1 { if (l1.slope() == l2.slope()) return 0; else return (l1.slope() > l2.slope()) ? +1 : -1; } /************************************************************* * I/O *************************************************************/ void read(Line2d l); void dump(Line2d l); }; // class Line2d // EXTRA // Line2d p_bisector(Point2d p, Point2d q);