Home > Back-end >  According to the coordinates for arbitrary polygon area
According to the coordinates for arbitrary polygon area

Time:09-16

Nonsense not say, see code:
#include
#include
Using namespace STD.
The class Area
{
Public:
Void (In);
Void Calucate ();
Void Out ();
Private:
Int N;
Float sum;
Float x1, x2, x3, y1, y2, y3.
};
Void Area: : (In)
{
sum=0;
Cout & lt; <"Input polygon vertices:";
Cin & gt;> N;
Cout & lt; <"Input coordinates according to the polygon vertex clockwise or counterclockwise input!" If (N==3)
{
Cout & lt; <"Please input the first vertex coordinates:";
Cin & gt;> The x1 & gt;> Y1.
Cout & lt; <"Please enter the second vertex coordinates:";
Cin & gt;> X2 & gt;> Y2.
Cout & lt; <"Please enter the third vertex coordinates:";
Cin & gt;> The x3 & gt;> Y3.
Calucate ();
} the else
{
Cout & lt; <"Please input the first vertex coordinates:";
Cin & gt;> The x1 & gt;> Y1.
Cout & lt; <"Please enter the second vertex coordinates:";
Cin & gt;> X2 & gt;> Y2.
Cout & lt; <"Please enter the third vertex coordinates:";
Cin & gt;> The x3 & gt;> Y3.
Calucate ();
For (int I=3; I{
Cout & lt; <"Please enter the first" & lt; If ((I + 1) % 2==0)
{
Cin & gt;> X2 & gt;> Y2.
Calucate ();
} the else
{
Cin & gt;> The x3 & gt;> Y3.
Calucate ();
}
}
}
}
Void Area: : Calucate ()
{
Float Cir, L1, L2, L3;
L1=SQRT ((x1, x2) * (x1, x2) + (y1, y2) * (y1, y2));
L2=SQRT ((x1 - x3) * (x1 - x3) + (y1, y3) * (y1, y3));
L3=SQRT ((x2, x3) * (x2 x3) + (y2, y3) * (y2, y3));
Cir=(L1 + L2 + l)/2;
The sum +=SQRT (Cir * (Cir - L1) * (Cir - L2) * (Cir - L3));
}
Void Area: : Out ()
{
Cout & lt; <"The polygon area is:" & lt; }
Int main ()
{
Area polygon;
Polygon. (In);
Polygon. Out ();
Return 0;
}
A defect but this method is that the input polygon vertex coordinates must be clockwise or counterclockwise input, otherwise will calculation error,

CodePudding user response:

How can I solve random input coordinates rather than clockwise or counterclockwise,

CodePudding user response:

The
reference 1/f, Tony C... Response:
could you tell me how to solve the random input coordinates rather than clockwise or counterclockwise,



I think, in front of the calculation, according to the x or y, row a point of order is ok,

CodePudding user response:

You study the theory of the formula before you


What sequence represents the

It means to one side, in the order,
That the shape of a polygon or the original? !
Area may also be the same? !

CodePudding user response:

Of four points, a rectangular, 1, 2, 3, 4, if be counter-clockwise, you change to 1 2 3 4, what is the result of a funnel shape, the area can be the same? !

CodePudding user response:

Random order input area is correct?
The polygon is convex? Why don't you check the convex hull algorithm?
  • Related