cours_progra/q2/algo/tp2/Droite.java

61 lines
1.5 KiB
Java
Raw Permalink Normal View History

2023-03-09 11:38:23 +01:00
public class Droite {
public double a, b, c;
public String toString(){
return a + "x + " + b + "y = " + c;
}
/*
* Represent a line in the space with the equation ax + by = c
* @param a ax
* @param b by
* @param c c
*/
Droite(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
/*
* Check if the line is horizontal
* @return true if the line is horizontal
* @return false if the line is not horizontal
*/
public boolean isHorizontal(){
if(a == 0)
return true;
return false;
}
/*
* Find the intersection by 2 lines
* @param other the other line you should find the intersection with
* @return Point The Point of the intersection
* @return Null if the lines are not intersecting
* @return Null if the lines are the sames
*/
public Point intersection(Droite other){
if(a * other.b - b * other.a == 0)
return null;
return new Point((other.b*c - b*other.c)/(a*other.b + b * other.a), (other.c - other.a*((other.b*c - b*other.c)/(a*other.b + b * other.a)))/other.b );
}
/*
* Create a new line with the two points given
* @param p1 the first point
* @param p2 the second point
* @return Droite the line that is between the given points
*/
public static Droite create(Point p1, Point p2){
System.out.println(p1 + "; " + p2);
Point directeur = new Point(p2.getX() - p1.getX(), p2.getY() - p2.getY());
System.out.println(directeur);
double a = -directeur.getY();
double b = directeur.getX();
return new Droite(a, b, a*p1.getX() + b*p1.getY());
}
}