The relationship between any quadrilateral area and the associated Triangle Area in C Language

Source: Internet
Author: User

Theorem: For any quadrilateral ABCD, the midpoint of the diagonal line AC and BD is M, N, AB, and CD. verify that the area of the triangle RMN is 1/4 of the area of the Quadrilateral ABCD.

Next we will use the C language to verify the correctness of this theorem. Because the computer's accuracy is limited, we use double precision to store the length of each side and the variables in the calculation process, because the double value intercepts the actual length, especially when the length is the root value, it leads to a discrepancy with the actual length. Therefore, the final area can only be approximate, that is, if the answer is close to 4, it should be the correct answer, and the original proposition will be proved.

# Include <stdio. h>
# Include <math. h>

Typedef struct
{
Double x;
Double y;
} Point;
 
# Define a P [0]
# Define B P [1]
# Define c P [2]
# Define d P [3]

 

Int OK (Point * p)/* pointer can be used as an array */
{/* Check if 1), 2) + pow (a2-a1), 2 );
Parallelogram. If 0 */is returned */
Double a0, b0, a1, b1, a2, b2, a3, b3, k1, k2, k3, k4;
A0 = p [0]. x; b0 = p [0]. y; a1 = p [1]. x; b1 = p [1]. y;
A2 = p [2]. x; b2 = p [2]. y; a3 = p [3]. x; b3 = p [3]. y;
K1 = pow (b1-b0), 2) + pow (a1-a0), 2 );
K2 = pow (b3-b2), 2) + pow (a3-a2), 2 );
K3 = pow (b2-b1), 2) + pow (a2-a1), 2 );
K4 = pow (b3-b0), 2) + pow (a3-a0), 2 );
If (k1 = k2) & (k3 = k4 ))
{Getch ();
Return 0;
}
Return 1;
}
/* Declare each sub-function */
Double si_area (double s []);
Point joind (Point P []);
Main ()
{
Point m, n, r, dian1, P [4], * PP = P;
Char * p;
Double x, y, tt, sos, var [4];
Double m1, m2, m3, tmp;
Static char pname [] = "ABCD ";
P = pname;
While (* p)
{
Printf ("input (x, y) of point % c:", * p );
Scanf ("% lf", & x, & y );
/* Scanf ("% lf", & PP-> x, & PP-> y); this input can be compiled successfully, but the runtime window will close */
PP-> x = x; PP-> y = y;
P ++; PP ++;

}/* Obtain the coordinates of the four vertices of the Quadrilateral */
If (! OK (P ))
{
Fprintf (stderr, "\ ninvalid input... \ n ");
Exit (1);/* end of exception 1 */
}/* Exit if AB/CD cannot be used together */


/* Calculate the vertices of a triangle */
M. x = (a. x + c. x)/2.0; m. y = (a. y + c. y)/2.0;
N. x = (B. x + d. x)/2.0; n. y = (B. y + d. y)/2.0;


/* Calculate the length of the four sides */
Var [0] = sqrt (pow (B. y-a.y), 2) + pow (B. x-a.x), 2 )));
Var [1] = sqrt (pow (c. y-b.y), 2) + pow (c. x-b.x), 2 )));
Var [2] = sqrt (pow (d. y-c.y), 2) + pow (d. x-c.x), 2 )));
Var [3] = sqrt (pow (d. y-a.y), 2) + pow (d. x-a.x), 2 )));
/* Calculate the area of the Quadrilateral */
Tt = si_area (var );

Dian1 = joind (P);/* obtain the intersection coordinate of the Quadrilateral */

R. x = dian1.x; r. y = dian1.y;
Printf ("the point is % lf \ n", r. x, r. y );
/* Calculate the area of a triangle by using the cosine theorem */
M1 = sqrt (pow (m. y-n.y), 2) + pow (m. x-n. x), 2 ));
M2 = sqrt (pow (r. y-n.y), 2) + pow (r. x-n. x), 2 ));
M3 = sqrt (pow (r. y-m.y), 2) + pow (r. x-m. x), 2 ));
Tmp = cos (pow (m1, 2) + pow (m2, 2)-pow (m3, 2)/2 * m1 * m2 );
Sos = m1 * m2 * sqrt (1-pow (tmp, 2 ));
Printf ("the retangle's area is % lf \ n", tt );
Printf ("the m n r is \ n % lf \ n", m. x, m. y, n. x, n. y, r. x, r. y );
Printf ("the triangle's area is % lf \ n", sos );
Printf ("the retangle's area is the % lf times as the triangle \ n", tt/sos );
Getch ();
Return 0;
}
/* The following is a function used to determine whether there is an intersection between two straight lines.
Return parameter: NULL. No parameters are returned because global variables are set.
Input parameter: four vertices of two line segments of the struct type */
Point joind (Point P [])
{
Double a0, b0, a1, b1, a2, b2, a3, b3, k1, k2;
Point ss;
Double dian [2];
A0 = P [0]. x; b0 = P [0]. y; a1 = P [1]. x; b1 = P [1]. y;
A2 = P [2]. x; b2 = P [2]. y; a3 = P [3]. x; b3 = P [3]. y;
If (a0-a1) = 0 & (b2-b3) = 0)
{
Dian [0] = a0;
Dian [1] = b2;
Printf ("the point is % lf \ n", dian [0], dian [1]);
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else if (a0-a1) = 0 & (b2-b3 )! = 0)
{
If (a2-a3) = 0)/* parallel line compared */
{
If (a1-a2) = 0)
{
Dian [0] = a1;
Dian [1] = (b3-b0) * (a1-a0)/(a3-a0) + b0;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;

}
Else if (a3-a0) = 0)
{
Dian [0] = a0;
Dian [1] = (b2-b1) * (a0-a1)/(a2-a1) + b1;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else
{
K1 = (b2-b1)/(a2-a1 );
K2 = (b0-b3)/(a0-a3 );
Dian [0] = (k2 * a3-k1 * a2 + b2-b3)/(k2-k1 );
Dian [1] = (dian [0]-a2) * k1 + b2;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
}
Else
{
K2 = (b2-b3)/(a2-a3 );
Dian [0] = a0;
Dian [1] = (a0-a2) * k2 + b2;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}

}
Else if (a0-a1 )! = 0 & (b2-b3) = 0)
{
If (b0-b1) = 0)/* parallel line compared */
{
If (a1-a2) = 0)
{
Dian [0] = a1;
Dian [1] = (b3-b0) * (a1-a0)/(a3-a0) + b0;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else if (a3-a0) = 0)
{
Dian [0] = a0;
Dian [1] = (b2-b1) * (a0-a1)/(a2-a1) + b1;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else
{
K1 = (b2-b1)/(a2-a1 );
K2 = (b0-b3)/(a0-a3 );
Dian [0] = (k2 * a3-k1 * a2 + b2-b3)/(k2-k1 );
Dian [1] = (dian [0]-a2) * k1 + b2;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
}
Else
{
K1 = (b0-b1)/(a0-a1 );
Dian [1] = b2;
Dian [0] = (b2-b0)/k1 + a0;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
}
Else
{
K1 = (b0-b1)/(a0-a1 );
K2 = (b2-b3)/(a2-a3 );
If (k1 = k2)/* compare the parallel line */
{
If (a1-a2) = 0)
{
Dian [0] = a1;
Dian [1] = (b3-b0) * (a1-a0)/(a3-a0) + b0;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else if (a3-a0) = 0)
{
Dian [0] = a0;
Dian [1] = (b2-b1) * (a0-a1)/(a2-a1) + b1;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
Else
{
K1 = (b2-b1)/(a2-a1 );
K2 = (b0-b3)/(a0-a3 );
Dian [0] = (k2 * a3-k1 * a2 + b2-b3)/(k2-k1 );
Dian [1] = (dian [0]-a2) * k1 + b2;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
}
Else
{
Dian [0] = (k1 * a0-k2 * a2 + b2-b0)/(K1-K2 );
Dian [1] = b0 + (dian [0]-a0) * k1;
Ss. x = dian [0]; ss. y = dian [1];
Return ss;
}
}
}
/* Calculate the area of the Quadrilateral */
Double si_area (double s [])
{
Double t1, a1;
T1 = (s [0] + s [1] + s [2] + s [3])/2.0;
A1 = sqrt (t1-s [0]) * (t1-s [1]) * (t1-s [2]) * (t1-s [3]);
Return a1;
}

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.