0

When I was using Python I created a program using basic knowledge of object orientation to compare between different cars.

now when i switch to java I come up with a plan to compare lines I have created a point class and get the lines as well but now i cant think of how should i compare if lines are collinear parallel perpendicular or just intersecting. any Ideas guys?

This Question has been **Answered**

Featured Replies

- 2
Well I see that you have specified a line segment to have two end points. Now to check if lines are collinear you will have to get their equations and solve them simultaneously. Now the equation of a line with slope "m" and with point (x1,y1) on it is given … Read More

0

Honestly there isn't enough information to suggest anything other than wild guesses.

Can you tell us the structure of your Point class, Line class etc, so that would help us see the problem from your perspective.

0

```
[code=java]
public class Point {
private double x;
private double y;
public Point(double xCoordinate, double yCoordinate)
{
this.x = xCoordinate;
this.y = yCoordinate;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
```

0

my line segment class and main is underconstruction

and I have one more question i dont have static methods so when i am calling them in main its saying non-static ..... some error, is there anyway i can fix it. One of my friend said make new method and call everything there and then call that method in main then again it says that method is not static. what do i do.

my line segment class

```
[code=java]
public class LineSeg{
// these are the attributes of a LineSeg object
private Point end1;
private Point end2;
private double length;
// the constructor
//@SuppressWarnings("static-access")
public LineSeg(double x1, double y1, double x2, double y2) {
end1 = new Point(x1,y1);
end2 = new Point(x2,y2);
this.length=(Math.sqrt(Math.pow(x2-x1,2))+(Math.pow(y2-y1,2)));
}
static double Slope(double x1,double y1, double x2, double y2){
double m=0;
m = (y1- y2)/( x1- x2);
return m;
}
public double getLength(){
return this.length;
}
}
```

0

If you can OO compare cars it should be easy to compare lines.

What have you got so far?

EDIT: sorry this was just posted after yours...

0

the problem is when I compared cars I have solid numbers like model year numbers of miles and price. here I just have two end points of a line.

0

From what I see you already have this should not be so hard.

For instance if two lines are parralel they must have the same slopes, if they also share a point they must be collinear.

0

yeah i know but two lines can have same slope but have two different end points and can go on the same line but my concern is only have two points how can i get the point of intersection between those lines

2

Well I see that you have specified a line segment to have two end points.

Now to check if lines are collinear you will have to get their equations and solve them simultaneously.

Now the equation of a line with slope "m" and with point (x1,y1) on it is given by :-

y = m(x-x1) + y1

You will need to define data members in your LineSegment class to store this information.

Once you have got the equations for all your line segments, to figure out if they are collinear or just intersecting you will have to solve the equation of any two of the line simultaneously.

For example consider we have three lines with the equations:-

Line 1: 3x + 4y = 3

Line 2: 2x - 2y = 7

Line 3: 9x -y = 0

You will need to solve any two of the equations simultaneously, to find the intersection points of the lines. That is if you solve the equations of Line 2 and Line 3 simultaneously you will get the intersection point of Line 2 and Line 3.

Next if you solve the equation of Line 1 and Line 3 simultaneously you will get the intersection point of Line 1 and Line 3.

If the intersection points in both the cases (Line 2 & 3 and Line 1 & 3) are both the same, then it means that the three lines are collinear and intersect at that point.

If on the other hand the point in both those cases is not the same then it means they are just intersecting lines.

Parallel lines have the same slope and for perpendicular lines the product of their slope is -1, so it should be easer to trace that.

<EDIT>

In case you need help solving simultaneous equations of lines to get the intersection points, you can see this page for help

yeah i know but two lines can have same slope but have two different end points and can go on the same line

Even in this case both those line segments would have the same equation, So this problem too should be solved.

0

Man you are genius I did that 2-3 years ago in calculus class and forgot everything now lets what I can get. thanks man

0

back again

now I am creating the LineEquation class please help me out

```
[code=java]
public class LineEquation
{
private double slope;
private double x;
private double y;
private double x1;
private double y1;
public LineEquation(double slope,double x,double y,double x1,double y1){
this.slope=slope;
this.x=x;
this.y=y;
this.x1=x1;
this.y1;
}
} //End of class
```

here is my LineSegment class

```
public class LineSeg{
// these are the attributes of a LineSeg object
private Point end1;
private Point end2;
private double length;
private double slope;
// the constructor
public LineSeg(double x1, double y1, double x2, double y2) {
end1 = new Point(x1,y1);
end2 = new Point(x2,y2);
this.length = length;
length=Math.sqrt((Math.pow(x2-x1,2))+(Math.pow(y2-y1,2)));
}
public double Slope(double x1, double y1, double x2, double y2){
slope= (double)(y2-y1)/(x2-x1);
return slope;
}
public double getLength(){
return length;
}
//public double EquationLine(double slope,double x1,double y1,double x,double y){
//}
}
```

0

My suggestion for your Line equation class is, do not store it directly as :-

y = m(x-x1) + y1

Instead store it in a reduced format as

y = mx + c

or

mx - y = - c --> Preferred as you will see in the last point.

And as you can see c = (y1-mx1), and ensure that you reduce all common factors, that is if you have an equation as 2x+2y = 2, reduce it to x + y = 1 , so that whenever you have to use the equations either to solve them simultaneously or to just to compare them, you can do so directly without any extra processing.

Also I think you might be interested in how to solve linear equations simultaneously using the determinant method as that is an easier option to program in your code.

0

thanks man I am going to try that just tell me if I am on right track

0

```
[code=java]
import java.io.*;
public class LineMain
{
public static void main(String[] args)
{
// TODO code application logic here
TextReader data;
double x1,y1,x2,y2;//declaring variables
try//use try catch to avoid FileNotFound error
{ // Create the input stream.
data = new TextReader(new FileReader("points.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("Can't find file points.txt!");
return;// End the program by returning from main().
}
try {
// Read numbers from the input file and print them out
x1 = data.getDouble();
y1 = data.getDouble();
Point p1=new Point(x1,y1);// create point object
x2 = data.getDouble();
y2 = data.getlnDouble();
Point p2=new Point(x2,y2);// create point object
LineSeg seg1=new LineSeg(p1,p2);// create line object
System.out.println("The end points of line 1 are ("+seg1.getPoint1().getX()+","+seg1.getPoint1().getY()+") and ("+seg1.getPoint2().getX()+","+seg1.getPoint2().getY()+")");
System.out.println("The length of line segment 1 is: "+seg1.getLength());
System.out.println("The Slope of line segment 1 is: "+seg1.getSlope());
System.out.println();
x1 = data.getDouble();
y1 = data.getDouble();
Point p3=new Point(x1,y1);// create point object
x2 = data.getDouble();
y2 = data.getlnDouble();
Point p4=new Point(x2,y2);// create point object
LineSeg seg2=new LineSeg(p3,p4);// create line object
System.out.println("The end points of line 2 are ("+seg2.getPoint1().getX()+","+seg2.getPoint1().getY()+") and ("+seg2.getPoint2().getX()+","+seg2.getPoint2().getY()+")");
System.out.println("The length of line segment 2 is: "+seg2.getLength());
System.out.println("The Slope of line segment 2 is: "+seg2.getSlope());
System.out.println();
x1 = data.getDouble();
y1 = data.getDouble();
Point p5=new Point(x1,y1);// create point object
x2 = data.getDouble();
y2 = data.getlnDouble();
Point p6=new Point(x2,y2);// create point object
LineSeg seg3=new LineSeg(p5,p6);// create line object
System.out.println("The end points of line 3 are ("+seg3.getPoint1().getX()+","+seg3.getPoint1().getY()+") and ("+seg3.getPoint2().getX()+","+seg3.getPoint2().getY()+")");
System.out.println("The length of line segment 3 is: "+seg3.getLength());
System.out.println("The Slope of line segment 3 is: "+seg3.getSlope());
System.out.println();
System.out.println("Now we have the slope and length of lines and now we can compare them.\n");
// comparison of lines and check if they are intersecting, parallel, collinear or perpendicular
if (seg1.isParallel(seg2))
{
System.out.println(" The line segments 1 and 2 are parallel.");
}
else
{
System.out.println(" The line segments 1 and 2 are not parallel.");
}
if (seg1.isIntersect(seg2))
{
System.out.println(" The line segments 1 and 2 are intersecting.");
}
else
{
System.out.println(" The line segments 1 and 2 are not intersecting.");
}
if (seg1.isCollinear(seg2))
{
System.out.println(" The line segments 1 and 2 are Collinear.");
}
else
{
System.out.println(" The line segments 1 and 2 are not collinear.");
}
if (seg1.isPerpendicular(seg2))
{
System.out.println(" The line segments 1 and 2 are Perpendicular.");
}
else
{
System.out.println(" The line segments 1 and 2 are not perpendicular.\n");
}
if (seg1.isParallel(seg3))
{
System.out.println(" The line segments 1 and 3 are parallel.");
}
else
{
System.out.println(" The line segments 1 and 3 are not parallel.");
}
if (seg1.isIntersect(seg3))
{
System.out.println(" The line segments 1 and 3 are intersecting.");
}
else
{
System.out.println(" The line segments 1 and 3 are not intersecting.");
}
if (seg1.isCollinear(seg3))
{
System.out.println(" The line segments 1 and 3 are collinear.");
}
else
{
System.out.println(" The line segments 1 and 3 are not collinear.");
}
if(seg1.isPerpendicular(seg3))
{
System.out.println(" The line segments 1 and 3 are perpendicular.\n");
}
else
{
System.out.println(" The line segments 1 and 3 are not perpendicular.\n");
}
if (seg2.isParallel(seg3))
{
System.out.println(" The line segments 2 and 3 are parallel.");
}
else
{
System.out.println(" The line segments 2 and 3 are not parallel.");
}
if (seg2.isIntersect(seg3))
{
System.out.println(" The line segments 2 and 3 are intersecting.");
}
else
{
System.out.println(" The line segments 2 and 3 are not intersecting.");
}
if (seg2.isCollinear(seg3))
{
System.out.println(" The line segments 2 and 3 are collinear.");
}
else
{
System.out.println(" The line segments 2 and 3 are not collinear.");
}
if(seg2.isPerpendicular(seg3))
{
System.out.println(" The line segments 2 and 3 are perpendicular.");
}
else
{
System.out.println(" The line segments 2 and 3 are not perpendicular.");
}
}
catch (IOException e)
{
// Some problem reading the data from the input file.
System.out.println("Input Error: " + e.getMessage());
}
finally
{
// Finish by closing the file, whatever else may have happened.
data.close();
}
}//end of main
}//end of class
public class LineSeg{
// these are the attributes of a LineSeg object
private Point end1;
private Point end2;
private double length;
// the constructor
public LineSeg(Point p1,Point p2)
{
setPoints(p1,p2);//call setpoints because
//it does the same which was suppose to be in constructor
}
private double length(Point p1,Point p2)//passing parameter to calculate length
{
return Math.sqrt((Math.pow(p2.getX()-p1.getX(),2))+(Math.pow(p2.getY()-p1.getY(),2)));
}
public double getSlope()//calculating and returning slope...
{
return(double)(end2.getY()-end1.getY())/(end2.getX()-end1.getX());
}
public Point getPoint1()
{
return end1;
}
public Point getPoint2()
{
return end2;
}
public double getLength()
{
return length;
}
public void setPoints(Point p1,Point p2){//set method to store instance values
this.end1 = p1;
this.end2 = p2;
this.length=length(p1,p2);
}
//Boolean methods to compare lines
public boolean isIntersect(LineSeg seg)
{
boolean intersect=false;
// formula extracted from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
double denominator1 = (this.getPoint1().getX()-this.getPoint2().getX());
double denominator2= (seg.getPoint1().getY()-seg.getPoint2().getY());
double denominator3= (this.getPoint1().getY()-this.getPoint2().getY());
double denominator4= (seg.getPoint1().getX()-seg.getPoint2().getX());
double denominator=(denominator1*denominator2)-(denominator3*denominator4);
if (denominator!=0)
{
intersect=true;
}return intersect;
}
public boolean isCollinear(LineSeg seg)
{
boolean collinear=false;
// formula extracted from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
double numerator1a=(seg.getPoint2().getX()-seg.getPoint1().getX());
double numerator1b=(this.getPoint1().getY()-seg.getPoint1().getY());
double numerator1c=(seg.getPoint2().getY()-seg.getPoint1().getY());
double numerator1d=(this.getPoint1().getX()-seg.getPoint1().getX());
double numerator1 =(numerator1a*numerator1b)-(numerator1c*numerator1d);
double numerator2a=(this.getPoint2().getX()-this.getPoint1().getX());
double numerator2b=(this.getPoint1().getY()-seg.getPoint1().getY());
double numerator2c=(this.getPoint2().getY()-this.getPoint1().getY());
double numerator2d=(this.getPoint1().getX()-seg.getPoint1().getX());
double numerator2=(numerator2a*numerator2b)-(numerator2c*numerator2d);
double d1 = (this.getPoint1().getX()-this.getPoint2().getX());
double d2= (seg.getPoint1().getY()-seg.getPoint2().getY());
double d3= (this.getPoint1().getY()-this.getPoint2().getY());
double d4= (seg.getPoint1().getX()-seg.getPoint2().getX());
double d=(d1*d2)-(d3*d4);
if (numerator1==0 && numerator2==0 && d==0)
{
collinear=true;
}
return collinear;
}
public boolean isParallel(LineSeg seg)
{
boolean parallel = false;
// formula extracted from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
double denominator1 = (this.getPoint1().getX()-this.getPoint2().getX());
double denominator2= (seg.getPoint1().getY()-seg.getPoint2().getY());
double denominator3= (this.getPoint1().getY()-this.getPoint2().getY());
double denominator4= (seg.getPoint1().getX()-seg.getPoint2().getX());
double denominator=(denominator1*denominator2)-(denominator3*denominator4);
if (denominator==0)
{
parallel=true;
}
return parallel;
}
public boolean isPerpendicular(LineSeg seg)
{
return(this.getSlope() * seg.getSlope()==-1);
}
}// end of class
public class Point {
private double x;
private double y;
public Point(double xCoordinate, double yCoordinate)
{
this.x = xCoordinate;
this.y = yCoordinate;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
```

here is what I did with my program do you guys have any feedbacks to make it better.

This question has already been answered. Start a new discussion instead.

Recommended Topics

I have a DGV (dgvListOfJobs), populated from an SQL database to show the user a list of 'jobs' (i.e. rows). I also show 7 other DGV's (dgvMonday, dgvTuesday, etc) ...

Hello,

I was wondering if it was possible to create a software like visual studio (in addition simple, obviously). We could program in vb.net, c ++, etc.