JAVA method for finding the intersection of two lines and the center of a triangle

  • 2020-04-01 02:26:26
  • OfStack

One, find the point where two lines intersect


class Point {
    double x;
    double y;

    public Point() {
        this.x = 0;
        this.y = 0;
    }
}
class Line {
    Point a;
    Point b;

    public Line() {
        this.a = new Point();
        this.b = new Point();
    }
    //So if you take the intersection of these two lines, if the slope is the same, then res is equal to u.a
    Point intersection(Line u,Line v){
        Point res = u.a;
        double t = ((u.a.x-v.a.x)*(v.b.y-v.a.y)-(u.a.y-v.a.y)*(v.b.x-v.a.x))
            /((u.a.x-u.b.x)*(v.b.y-v.a.y)-(u.a.y-u.b.y)*(v.b.x-v.a.x));
        res.x += (u.b.x-u.a.x)*t;
        res.y += (u.b.y-u.a.y)*t;
        return res;
    }

Two, find the center of the triangle
1. Perpendicular: the heights of the three sides of a triangle intersect at one point. This point is called the perpendicular of the triangle.
Center of gravity: the center line of the three sides of the triangle crosses at a point called the center of gravity of the triangle.
3. Outer center: the center perpendicular of the three sides of the triangle intersects at a point, which is the center of the circle of the triangle.
The bisector of the inner triangle intersects at a point that is the center of the tangent circle of the triangle.
Known round 3 points, first find out 3 side length, area is obtained by Helen formula S = SQRT (p * (p - a) * (p - b) * (p - c)) p = (a + b + c) / 2; According to the triangle area formula S=1/2*a*b*sin(C) and the sine theorem a/sin(a)=b/sin(b)= C /sin(C)= diameter (according to the same length of the string corresponding to the same circular Angle), the diameter =a*b* C /2/S can be obtained.
Find the center coordinates. Use: the sufficient and necessary condition for G to be the external center of ABC is that (vector GA+ vector GB) , vector AB= (vector GB+ vector GC) , vector BC=(vector GC+ vector GA) , vector CA= vector 0.
It's easy to prove this property, just by thinking that the center of the outside is the intersection of the center perpendicular, and you can prove this property by using vectors to avoid finding the slope, and by thinking that the slope doesn't exist and so on.


//Center of triangle circumferential circle
    Point center(Point a,Point b,Point c) {
        //Plus this is why the compiler doesn't tell you not to initialize, because new also writes the constructor
        Line u = new Line(),v = new Line();
        u.a.x=(a.x+b.x)/2;
        u.a.y=(a.y+b.y)/2;
        u.b.x=u.a.x+(u.a.y-a.y);
        u.b.y=u.a.y-(u.a.x-a.x);
        v.a.x=(a.x+c.x)/2;
        v.a.y=(a.y+c.y)/2;
        v.b.x=v.a.x+(v.a.y-a.y);
        v.b.y=v.a.y-(v.a.x-a.x);
        return intersection(u,v);
    }


Three. Find the heart of the triangle
              Since the inner distance to each side is the radius r, the triangle can be divided into three parts, and then according to Helen's formula, the radius r = 2 * S/(a + b + c).
              Incenter coordinates (x, y) : three triangle vertex coordinates: A (x1, y1), B (x2, y2), C (x3, y3) center for x = (x1 + x2 * * BC CA + x3 * AB)/(AB + BC + CA), y = (* BC + y1 y2 * CA + y3 * AB)/(AB + BC + CA).
              Proof: the heart is the intersection of angular bisectors, the distance to the three sides is equal.
Suppose: in triangle ABC, the coordinates of the three vertices are: A(x1,y1),B(x2,y2),C(x3,y3) BC= A,CA= B,AB= C, inner M (X,Y), then aMA+bMB+cMC=0 (three vectors), MA= (x1 -X, y1 -Y), MB=(x2 -X, y2 -Y), MC=(x3 -X, y3 -Y).
Is: a (X1 - X) + b (X2 - X) + c (X3 - X) = 0, a (Y1 - Y) + b (Y2 - Y) + c (Y3 - Y) = 0
X= (aX1+bX2+cX3)/(a+b+c), Y= (aY1+bY2+cY3)/(a+b+c)
M (aX1+bX2+cX3)/(a+b+c), (aY1+bY2+cY3)/(a+b+c)).

It is known that O is the heart of triangle ABC, and a,b and c are the length of the opposite side of A.B.C. aOA+bOB+cOC=0(OA,OB,OC all refer to vectors)

Proof: let the triangle ABC, AD be the angular bisector on the side of BC, and inner O.
|, BC, |=a, |, AC, |=b, |, AB, |=c
BOB aOA + + cOC
OA = aOA + (AB + b + c + OA (AC)
The OA = (a + b + c) + b (DB - DA) + c (DC - DA)
Set the direction vector e of BC, then DB=e|, DB|,DC=-e|,DC |
By the Angle bisector theorem, |, DB, |/|, DC, |=c/b, so bDB+cDC=0
OA (a + b + c) + b (DB - DA) + c (DC - DA) = (a + b + c) OA - DA - DA = aOA c + b (b + c) OD
Because OA and OD are reversed, the Angle bisector theorem and the ratio theorem are used:
B/CD/BD = = c (b + c)/(CD + BD) = (b + c)/a, b = OA/OD/CD,
So OA/OD is equal to (b+c)/a, and because OA and OD are reversed,
So the aOA cOC = aOA + + bOB + (b + c) OD = 0.


Related articles: