// // 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-projective.cpp // // Description: Compute the potential intersection point of two line segments using // the cross-product: cross-product for defining supporting lines and cross-product // of the line vectors for determining the intersection projective point. // ------------------------------------------------------------------------ #include "stdafx.h" #include #include #include #include using namespace std; #define W 800 #define H 800 // Duality #define Line2D Point2D inline double drand() {return rand()/(double)RAND_MAX;} class Point2D{public: double x,y,w; Point2D::Point2D(double xx, double yy) { x=xx; y=yy; w=1.0; } Point2D::Point2D() {x=y=0.0; w=1.0;} // Dehomogenization (perspective division) void Normalize() { if (w!=0) {x/=w; y/=w; w=1.0;} } void Rand() { x=drand();y=drand();w=1.0; } friend ostream & operator << (ostream & os, const Point2D p) { os<<"["<q.x) swap(p,q); // 2nd segment r.Rand(); s.Rand(); if (r.x>s.x) swap(r,s); // Directional vectors u1.x=q.x-p.x; u1.y=q.y-p.y; norm=sqrt(u1.x*u1.x + u1.y*u1.y); u1.x/=norm;u1.y/=norm; u1.w=1.0; u2.x=s.x-r.x; u2.y=s.y-r.y; norm=sqrt(u2.x*u2.x+u2.y*u2.y); u2.x/=norm;u2.y/=norm; u2.w=1.0; //cout<=p.x)&&(intersection.x<=q.x)&&(intersection.x>=r.x)&&(intersection.x<=s.x)) intersect=true; else intersect=false; if (intersect) cout<<"Line segments intersect."<