# geometry

By: a guest on Aug 7th, 2012  |  syntax: Java  |  size: 13.21 KB  |  hits: 25  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. /*
2.   File: Geometry.java
3.
4.   Description:
5.
6.   Student Name:
7.
8.   Student UT EID:
9.
10.   Partner's Name:
11.
12.   Partner's UT EID:
13.
14.   Course Name: CS 312
15.
16.   Unique Numbers: 91035
17.
18.   Date Created: 08/06/2012
19.
21.
22. */
23.
24. import java.util.*;
25. import java.io.*;
26.
27. class Point
28. {
29.   // list of attributes - x and y coordinates
30.   private double x;
31.   private double y;
32.
33.   // default constructor
34.   public Point ()
35.   {
36.     x = 0.0;
37.     y = 0.0;
38.   }
39.
40.   // non-default constructors
41.   public Point (double x, double y)
42.   {
43.     this.x = x;
44.     this.y = y;
45.   }
46.
47.   public Point (Point p)
48.   {
49.     x = p.getX();
50.     y = p.getY();
51.   }
52.
53.   // accessors get the x and y coordinates
54.   public double getX ()
55.   {
56.     return x;
57.   }
58.   public double getY ()
59.   {
60.     return y;
61.   }
62.
63.   // mutators set the x and y coordinates
64.   public void setX (double x)
65.   {
66.     this.x = x;
67.   }
68.   public void setY (double y)
69.   {
70.     this.y = y;
71.   }
72.
73.   // test for equality of two doubles
74.   public boolean isEqual (double x, double y)
75.   {
76.     double delta = 1.0e-13;    // an arbitrary small number
77.     return (((Math.abs (x - y)) < delta));
78.   }
79.
80.   // distance to another point
81.   public double distance (Point p)
82.   {
83.     return Math.sqrt ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
84.   }
85.
86.   // string representation of a point, i.e. x and y coordinates
87.   public String toString ()
88.   {
89.     return "(" + x + ", " + y + ")";
90.   }
91.
92.   // test for equality of two points
93.   public boolean equals (Point p)
94.   {
95.     return (isEqual(x, p.x) && isEqual(y, p.y));
96.   }
97. }
98.
99. class Line
100. {
101.   // list of attributes
102.   private double slope;
103.   private double intercept;
104.
105.   // default constructor (slope = 1 and intercept = 0)
106.   public Line ()
107.   {
108.     slope = 1.0;
109.     intercept = 0.0;
110.   }
111.
112.   // non-default constructors
113.   public Line (double slope, double intercept)
114.   {
115.     this.slope = slope;
116.     this.intercept = intercept;
117.   }
118.
119.   // define line if p and q are not the same
120.   public Line (Point p, Point q)
121.   {
122.     //slope = (y1-y2)/(x1-x2)
123.     //intercept = y1-slope*x
124.     this.slope = (p.getY() - q.getY())/(p.getX() - q.getX());
125.     this.intercept = p.getY() - slope * p.getX();
126.   }
127.
128.   // accessors
129.   public double getSlope ()
130.   {
131.     return slope;
132.   }
133.
134.   public double getIntercept ()
135.   {
136.     return intercept;
137.   }
138.
139.   // mutators
140.   public void setSlope (double slope)
141.   {
142.     this.slope = slope;
143.   }
144.
145.   public void setIntercept (double intercept)
146.   {
147.     this.intercept = intercept;
148.   }
149.
150.   // test for equality of two doubles
151.   public boolean isEqual (double x, double y)
152.   {
153.     double delta = 1.0e-13;    // an arbitrary small number
154.     return (((Math.abs (x - y)) < delta));
155.   }
156.
157.   // determine if two lines are parallel
158.   // same slopes <=> parallel
159.   public boolean isParallel (Line line)
160.   {
161.     return isEqual(this.slope, line.slope);
162.   }
163.
164.   // determine the intersection point if two lines are not parallel
165.   public Point intersectionPoint (Line line)
166.   {
167.     Point intersection = new Point();
168.     double m1 = this.getSlope();
169.     double m2 = line.getSlope();
170.     double b1 = this.getIntercept();
171.     double b2 = line.getIntercept();
172.     intersection.setX((b1-b2)/(m2-m1));
173.     intersection.setY(m1*intersection.getX() + b1);
174.     return intersection;
175.
176.   }
177.
178.   // determine if two lines are perpendicular to each other
179.   public boolean isPerpendicular (Line line)
180.   {
181.     //m1 = -(1/m2)
182.     return isEqual(this.getSlope(),-1 * line.getSlope());
183.   }
184.
185.   // determine the perpendicular distance of a point to the line
186.   public double distance (Point p)
187.   {
188.     double m = this.getSlope();
189.     double b = this.getIntercept();
190.     double x = p.getX();
191.     double y = p.getY();
192.     return Math.abs(m*x - y + b)/ Math.sqrt(m*m + 1);
193.
194.   }
195.
196.   // determine if two points are on the same side of the line
197.   // if one or both points are on the line return false
198.   public boolean onSameSide (Point p, Point q)
199.   {
200.     //side1 if y > mx + b; side2 if y < mx + b
201.     double x1 = p.getX();
202.     double y1 = p.getY();
203.     double x2 = q.getX();
204.     double y2 = q.getY();
205.     double m = this.getSlope();
206.     double b = this.getIntercept();
207.     if (y1 == m*x1 + b || y2 == m*x2 + b)
208.       return false;
209.     else
210.       return (y1 > m*x1 + b) == (y2 > m*x2 + b);
211.   }
212.
213.   // string representation of the slope and intercept of a line, e.g.
214.   // slope: 1.0 intercept: 0.0
215.   public String toString ()
216.   {
217.     return ("slope: " + slope + " intercept: " + intercept);
218.   }
219.
220.   // determine if two lines are equal, i.e. have the same slope
221.   // and intercept
222.   public boolean equals (Line line)
223.   {
224.     return(isEqual(this.getSlope(), line.getSlope()) &&
225.         isEqual(this.getIntercept(), line.getIntercept()));
226.   }
227. }
228.
229. class Triangle
230. {
231.   // list attributes
232.   private Point v1;
233.   private Point v2;
234.   private Point v3;
235.
236.   // default constructor creates a triangle having
237.   // vertices (0, 0), (1, 0), and (0, 1).
238.   public Triangle ()
239.   {
240.     v1 = new Point(0,0);
241.     v2 = new Point(1,0);
242.     v3 = new Point(0,1);
243.   }
244.
245.   // non-default constructors accept user defined points
246.   // and creates triangle object if the points form a
247.   // triangle or the default triangle if they do not.
248.   public Triangle (Point v1, Point v2, Point v3)
249.   {
250.     if (isTriangle(v1,v2,v3))
251.     {
252.       this.v1 = new Point(v1);
253.       this.v2 = new Point(v2);
254.       this.v3 = new Point(v3);
255.     }
256.   }
257.
258.   public Triangle (double x1, double y1,
259.       double x2, double y2,
260.       double x3, double y3)
261.   {
262.     if (isTriangle(x1, y1, x2, y2, x3, y3))
263.     {
264.       this.v1 = new Point(x1, y1);
265.       this.v2 = new Point(x2, y2);
266.       this.v3 = new Point(x3, y3);
267.     }
268.   }
269.
270.   // accessors
271.   public Point getVertex1 ()
272.   {
273.     return v1;
274.   }
275.   public Point getVertex2 ()
276.   {
277.     return v2;
278.   }
279.   public Point getVertex3 ()
280.   {
281.     return v3;
282.   }
283.
284.   // mutators reset the vertices only if the triangle shape
285.   // is preserved i.e. the points do not collapse to a line
286.   public void setVertex1 (Point v1)
287.   {
288.     if(isTriangle(v1, v2, v3))
289.     {
290.       this.v1 = v1;
291.     }
292.   }
293.   public void setVertex2 (Point v2)
294.   {
295.     if(isTriangle(v1, v2, v3))
296.     {
297.       this.v2 = v2;
298.     }
299.   }
300.   public void setVertex3 (Point v3)
301.   {
302.     if(isTriangle(v1, v2, v3))
303.     {
304.       this.v3 = v3;
305.     }
306.   }
307.
308.   public void setVertex1 (double x1, double y1)
309.   {
310.     if (isTriangle(x1, y1, v2.getX(), v2.getY(), v3.getX(), v3.getY()))
311.     {
312.       this.v1 = new Point(x1, y1);
313.     }
314.   }
315.
316.   public void setVertex2 (double x2, double y2)
317.   {
318.     if (isTriangle(v1.getX(), v1.getY(), x2, y2, v3.getX(), v3.getY()))
319.     {
320.       this.v2 = new Point(x2, y2);
321.     }
322.   }
323.
324.   public void setVertex3 (double x3, double y3)
325.   {
326.     if (isTriangle(v1.getX(), v1.getY(), v2.getX(), v2.getY(), x3, y3))
327.     {
328.       this.v3 = new Point(x3, y3);
329.     }
330.   }
331.
332.   // test for equality of two doubles
333.   public boolean isEqual (double x, double y)
334.   {
335.     double delta = 1.0e-13;    // an arbitrary small number
336.     return (((Math.abs (x - y)) < delta));
337.   }
338.
339.   // determines if three points form a triangle
340.   private boolean isTriangle (Point p1, Point p2, Point p3)
341.   {
342.     double x1 = p1.getX();
343.     double y1 = p1.getY();
344.     double x2 = p2.getX();
345.     double y2 = p2.getY();
346.     double x3 = p3.getX();
347.     double y3 = p3.getY();
348.     return isEqual((y2 - y1)/(x2-x1), (y3-y2)/(x3-x2));
349.   }
350.   private boolean isTriangle (double x1, double y1,
351.       double x2, double y2,
352.       double x3, double y3)
353.   {
354.     return isEqual((y2 - y1)/(x2-x1), (y3-y2)/(x3-x2));
355.   }
356.
357.   // calculates area of a triangle
358.   public double area ()
359.   {
360.     double base = getVertex1().distance(getVertex2());
361.     Line baseLine = new Line(v1,v2);
362.     double height = baseLine.distance(v3);
363.     return 1/2 * base * height;
364.   }
365.
366.   // calculates the perimeter
367.   public double perimeter ()
368.   {
369.     return v1.distance(v2) + v2.distance(v3) + v3.distance(v1);
370.   }
371.
372.   // determines if a point is inside the triangle
373.   public boolean isInside (Point p)
374.   {
375.     Line lineV1V2 = new Line (v1, v2);
376.     Line lineV2V3 = new Line (v2, v3);
377.     Line lineV1V3 = new Line (v1, v3);
378.     return lineV1V2.onSameSide(p, v3) && lineV2V3.onSameSide(p,v1)
379.         && lineV1V3.onSameSide(p,v2);
380.   }
381.
382.   // determines if the given triangle is completely inside Triangle t
383.   public boolean isInside (Triangle t)
384.   {
385.     return isInside(t.getVertex1()) && isInside(t.getVertex2())
386.         && isInside(t.getVertex3());
387.   }
388.
389.   // determines if the given triangle overlaps Triangle t,
390.   // if it shares some (or all) of its area with Triangle t
391.   public boolean doesOverlap (Triangle t)
392.   {
393.     return isInside(t.getVertex1()) || isInside(t.getVertex2())
394.     || isInside(t.getVertex3()) || //starOfDavid
395.   }
396.
397.   // determines if a line passes through the triangle
398.   public boolean doesIntersect (Line line)
399.   {
400.     Line lineV1V2 = new Line (v1, v2); //Fix this whole thing
401.     Line lineV2V3 = new Line (v2, v3);
402.     Line lineV1V3 = new Line (v1, v3);
403.     Point pV1V2 = line.intersectionPoint(lineV1V2);
404.     Point pV2V3 = line.intersectionPoint(lineV2V3);
405.     Point pV1V3 = line.intersectionPoint(lineV1V3);
406.     double xV1V2 = pV1V2.getX();
407.     double yV1V2 = pV1V2.getY();
408.     double xV1V3 = pV1V3.getX();
409.     double yV1V3 = pV1V3.getY();
410.     double xV2V3 = pV2V3.getX();
411.     double yV2V3 = pV2V3.getY();
412.     return ((xV1V2 < v1.x) == (xV1V2 > v2.x)) && (xV1V2 < v1.y) == (pV1V2.y > v2.y)) ||
413.         ((pV1V3.x < v1.x) == (pV1V3.x > v3.x)) && ((pV1V3.y < v1.y) == (pV1V3.y > v3.y)) ||
414.         ((pV1V2.x < v1.x) == (pV1V2.x > v2.x)) && ((pV1V2.x < v1.x) == (pV1V2.x > v2.x))
415.   }
416.
417.   // returns a string representation of a triangle
418.   // i.e. it gives the three vertices
419.   public String toString ()
420.   {
421.     return v1.toString() + ", " + v2.toString() + ", " + v3.toString();
422.   }
423.
424.
425.   // determines if two triangles are congruent, i.e. the
426.   // three sides of one are equal to three sides of the other
427.   public boolean equals (Triangle t)
428.   {
429.
430.   }
431.
432. }
433.
434. public class Geometry
435. {
436.
437.   public static void main (String[] args) throws IOException
438.   {
439.     // open file "geometry.txt" for reading
440.     File input = new File ("geometry.txt");
441.     Scanner sc = new Scanner(input);
442.
443.
444.     // read the coordinates of the first Point P
445.     String [] strArrayP = (sc.nextLine()).split(" ");
446.     Point p = new Point ( Double.parseDouble(strArrayP[0]),
447.         Double.parseDouble(strArrayP [1]));
448.     System.out.println("Coordinates of P: " + p.toString());
449.
450.     // read the coordinates of the second Point Q
451.     String [] strArrayQ = (sc.nextLine()).split(" ");
452.     Point q = new Point ( Double.parseDouble(strArrayQ[0]),
453.         Double.parseDouble(strArrayQ [1]));
454.     System.out.println("Coordinates of Q: " + q.toString());
455.
456.     // print distance between P and Q
457.     System.out.println("Distance between P and Q: " + p.distance(q));
458.
459.     // print the slope and intercept of the line passing through P and Q
460.     Line linePQ = new Line(p,q);
461.     System.out.println("Slope and Intercept of PQ: " + linePQ.toString());
462.
463.     // read the coordinates of the third Point A
464.     String [] strArrayA = (sc.nextLine()).split(" ");
465.     Point a = new Point ( Double.parseDouble(strArrayA[0]),
466.         Double.parseDouble(strArrayA [1]));
467.     System.out.println("Coordinates of A: " + a.toString());
468.
469.     // read the coordinates of the fourth Point B
470.     String [] strArrayB = (sc.nextLine()).split(" ");
471.     Point b = new Point ( Double.parseDouble(strArrayB[0]),
472.         Double.parseDouble(strArrayB [1]));
473.     System.out.println("Coordinates of B: " + b.toString());
474.
475.     // print the slope and intercept of the line passing through A and B
476.     Line lineAB = new Line(a,b);
477.     System.out.println("Slope and Intercept of AB: " + lineAB.toString());
478.
479.     // print if the lines PQ and AB are parallel or not
480.     System.out.println("PQ is " + (linePQ.isParallel(lineAB)? "":"not ")
481.         + "parallel to AB.");
482.
483.     // print if the lines PQ and AB are perpendicular or not
484.     System.out.println("PQ is " + (linePQ.isPerpendicular(lineAB)? "":"not ")
485.         + "perpendicular to AB.");
486.
487.     // print the coordinates of the intersection point if PQ is not
488.     // parallel to AB
489.     if (! lineAB.isParallel(linePQ))
490.     {
491.       System.out.println("Coordinates of intersection point of PQ and AB: "
492.           + lineAB.intersectionPoint(linePQ).toString());
493.     }
494.
495.     // read the coordinates of the fifth Point G
496.
497.     // read the coordinates of the sixth Point H
498.
499.     // print if the the points G and H are on the same side of PQ
500.
501.     // print if the the points G and H are on the same side of AB
502.
503.     // read the coordinates of the vertices R, S, and T
504.
505.     // print the perimeter of triangle RST
506.
507.     // print the area of triangle RST
508.
509.     // print if the line PQ passes through the triangle RST
510.
511.     // print if the line AB passes through the triangle RST
512.
513.     // read the coordinates of the vertices J, K, and L
514.
515.     // print if triangle JKL is inside triangle RST
516.
517.     // print if triangle JKL overlaps triangle RST
518.
519.     // print if triangle JKL is congruent to triangle RST
520.
521.     // close file "geometry.txt"
522.
523.   }
524.
525. }