0% found this document useful (0 votes)
2 views6 pages

Lab File

Ww

Uploaded by

uday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Lab File

Ww

Uploaded by

uday
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment-8

Line class
package Geometry.Shapes2D;
public class Line {
// Data members
private Point start; // Start point of line
private Point end; // End point of line
// Create a line from two points
public Line(final Point start, final Point end) {
this.start = new Point(start);
this.end = new Point(end);
}
// Create a line from two coordinate pairs
public Line(double xStart, double yStart, double xEnd, double yEnd) {
start = new Point(xStart, yStart); // Create the start point
end = new Point(xEnd, yEnd); // Create the end point
}
public Line(final Line aLine) {
start = aLine.start;
end = aLine.end;
}
// Calculate the length of a line
public double length() {
return start.distance(end); // Use the method from the
Point class
}
// Return a point as the intersection of two lines -- called from a Line object
public Point intersects(final Line line2) {
double num =(end.getY() - start.getY())*(start.getX() - line2.start.getX()) -
(end.getX() - start.getX())*(start.getY() - line2.start.getY());
double denom = (end.getY() - start.getY())*(line2.end.getX() - line2.start.getX())
-
(end.getX() - start.getX())*(line2.end.getY() - line2.start.getY());
double x = (line2.start.getX() + (line2.end.getX() -
line2.start.getX())*num/denom);
double y = (line2.start.getY() + (line2.end.getY() -
line2.start.getY())*num/denom);
Point p = new Point(x,y);
return p;
}
// Convert a line to a string
public String toString() {
return "(" + start+ "):(" + end + ")"; // As "(start):(end)"
} // that is, "(x1, y1):(x2, y2)"
public Point getStart(){
return start;
}
public Point getEnd(){
return end;
}
}
Point class
package Geometry.Shapes2D;
public class Line {
// Data members
private Point start; // Start point of line
private Point end; // End point of line
// Create a line from two points
public Line(final Point start, final Point end) {
this.start = new Point(start);
this.end = new Point(end);
}
// Create a line from two coordinate pairs
public Line(double xStart, double yStart, double xEnd, double yEnd) {
start = new Point(xStart, yStart); // Create the start point
end = new Point(xEnd, yEnd); // Create the end point
}
public Line(final Line aLine) {
start = aLine.start;
end = aLine.end;
}
// Calculate the length of a line
public double length() {
return start.distance(end); // Use the method from the
Point class
}
// Return a point as the intersection of two lines -- called from a Line object
public Point intersects(final Line line2) {
double num =(end.getY() - start.getY())*(start.getX() - line2.start.getX()) -
(end.getX() - start.getX())*(start.getY() - line2.start.getY());

double denom = (end.getY() - start.getY())*(line2.end.getX() - line2.start.getX())


-
(end.getX() - start.getX())*(line2.end.getY() - line2.start.getY());
double x = (line2.start.getX() + (line2.end.getX() -
line2.start.getX())*num/denom);
double y = (line2.start.getY() + (line2.end.getY() -
line2.start.getY())*num/denom);
Point p = new Point(x,y);
return p;
}
// Convert a line to a string
public String toString() {
return "(" + start+ "):(" + end + ")"; // As "(start):(end)"
} // that is, "(x1, y1):(x2, y2)"
public Point getStart(){
return start;
}
public Point getEnd(){
return end;
}
}
Trygeometry Class
import Geometry.Shapes2D.Point;
import Geometry.Shapes2D.Line;
import KeyboardRead.KeyboardRead;
public class TryGeometry {
public static void main(String[] args) {
double[] nums;
// Create two points and display them
System.out.println("Enter the x and y coordinates of the start point for line1 as
comma seperated fractional numbers: ");
nums = KeyboardRead.readDoubleArray();
Point start = new Point(nums[0], nums[1]);
System.out.println("Enter the x and y coordinates of the end point for line1 as
comma seperated fractional numbers: ");
nums = KeyboardRead.readDoubleArray();
Point end = new Point(nums[0], nums[1]);
System.out.println("Points created are " + start + " and " + end);
// Create two lines and display them
Line line1 = new Line(start, end);
Line line2 = new Line(0.0, 3.0, 3.0, 0.0);
System.out.println("Lines created are " + line1 + " and " + line2);
// Display the intersection
System.out.println("Intersection is " + line2.intersects(line1));
// Now move the end point of line1 and show the new intersection
System.out.println("End point of the line1 is being moved by 1 unit along x and
-5 units along y .");
Point p = line1.getEnd();
p.move(1.0, -5.0);
System.out.println("The new end point of line1 is: "+ p);
System.out.println("Intersection is " + line1.intersects(line2));
}
}
Output:

You might also like