// ------------------------------------------------------------------------ // This program is complementary material for the book: // // Frank Nielsen // // Visual Computing: Geometry, Graphics, and Vision // // ISBN: 1-58450-427-7 // // Charles River Media, Inc. // // // All programs are available at http://www.charlesriver.com/visualcomputing/ // // You may use this program for ACADEMIC and PERSONAL purposes ONLY. // // // The use of this program in a commercial product requires EXPLICITLY // written permission from the author. The author is NOT responsible or // liable for damage or loss that may be caused by the use of this program. // // Copyright (c) 2005. Frank Nielsen. All rights reserved. // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // File: segmentintersection-primitive.cpp // // Description: Define a predicate that report whether two line segments // intersect or not using Orient2D orientation predicates of triples of points. // ------------------------------------------------------------------------ #include "stdafx.h" #include #include #include using namespace std; // Orientation predicate results: #define CCW 1 #define ON 0 #define CW -1 #define W 800 #define H 800 // Random number from 0 to 1 inline double drand(){return rand()/(double)RAND_MAX;} class Point2D{ public: double x,y; friend ostream &operator<<(ostream & o, const Point2D & p) { o<<"("< (r.x-p.x)*(q.y-p.y)+ERR) return CCW; if ((q.x-p.x)*(r.y-p.y) < (r.x-p.x)*(q.y-p.y)-ERR) return CW; return ON; } class Segment2D{ public: Point2D a,b; Segment2D::RandomDraw() { a.x=drand();a.y=drand(); b.x=drand();b.y=drand(); // swap extremities if (a.x>b.x) swap(a,b); } friend ostream &operator<<(ostream & o, const Segment2D & s) { o<<"Segment ["<s2.a.x) swap(s1,s2); cout<