(2). [5 points] Write an intersection predicate, RayLineIntersectProp, in C (or
ID: 3587900 • Letter: #
Question
(2). [5 points] Write an intersection predicate, RayLineIntersectProp, in C (or Java or pseudocode) which takes four points, a, b, c, d, which define a ray, ab (with endpoint a and passing through point b a, going to infinity) and a line. cd, through points c and d c, and determines if they intersect properly or not. (The ray ab is considered to be closed it includes its endpoint a.) Here, intersection will mean that they have a proper intersection: there is eractly one point of the interior of ray ab that also lies on the line ed. (We do not consider them to intersect properly if they share exactly the endpoint a. We also do not consider them to intersect properly if they are collinear, since there intersection would then be the entire ray, rather than a single point.) You are allowed to assume that a b and that c* d Your predicate should start bool RayLineIntersectProp( tPointi a, tPointi b, tPointi c, tPointi d) [FILL IN HERE] You may use the predicates Area2, Left, LeftOn, Collinear, IntersectProp, Between, and Intersect from the text, but try to think carefully about making your code complete (handling all cases) and efficient (avoiding duplication of tests) You may find it helpful also to use the following predicate, CrossProduct /* Compute the cross product of vector ab and vector cd/ int CrossProduct( tPointi a, tPointi b, tPointi c, tPointi d ) returrn Note: Even if you have never programmed in C before, this exercise should be within your ability; try to read and understand the code fragments of section 1.5. If you are comfortable with C programming, you may find it convenient to test it by compiling it and executing the code (within a simple main program)Explanation / Answer
import java.util.*; import java.lang.*; import java.io.*; class Point { double x,y; public Point(double x, double y) { this.x = x; this.y = y; } // Method used to display X and Y coordinates // of a point static void displayPoint(Point p) { System.out.println("(" + p.x + ", " + p.y + ")"); } } class Test { static boolean RayLineIntersection(Point A, Point B, Point C, Point D) { // Line AB represented as a1x + b1y = c1 double a1 = B.y - A.y; double b1 = A.x - B.x; double c1 = a1*(A.x) + b1*(A.y); // Line CD represented as a2x + b2y = c2 double a2 = D.y - C.y; double b2 = C.x - D.x; double c2 = a2*(C.x)+ b2*(C.y); double crossproduct = crossproduct(A,B,C,D); double determinant = a1*b2 - a2*b1; if (crossproduct == 0) { // The lines are parallel. This is simplified // by returning a pair of FLT_MAX return false; } else { double x = (b2*c1 - b1*c2)/determinant; double y = (a1*c2 - a2*c1)/determinant; //check for perfect if(x>A.x && x>C.x&&y>A.y&& y>C.y ){ return true; } } return false; } static double crossproduct(Point a, Point b, Point c, Point d){ return ((b.x -a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x)); } // Driver method public static void main(String args[]) { Point A = new Point(1, 1); Point B = new Point(4, 4); Point C = new Point(1, 8); Point D = new Point(2, 4); Point intersection = RayLineIntersection(A, B, C, D); System.out.println(intersection); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.