Fractals/Computer graphic techniques/2D/algorithms: Difference between revisions
Appearance
[checked revision] | [checked revision] |
Content deleted Content added
→Testing if point is inside triangle: polygon |
m →Testing if point is inside triangle: orientation and area of the triangfle |
||
Line 115: | Line 115: | ||
</source> |
</source> |
||
==Orientation and area of the triangle== |
|||
Orientation and area of the triangle : how to do it ? |
|||
<source lang=c> |
|||
// gcc t.c -Wall |
|||
// ./a.out |
|||
# include <stdio.h> |
|||
// http://ncalculators.com/geometry/triangle-area-by-3-points.htm |
|||
double GiveTriangleArea(double xa, double ya, double xb, double yb, double xc, double yc) |
|||
{ |
|||
return ((xb*ya-xa*yb)+(xc*yb-xb*yc)+(xa*yc-xc*ya))/2.0; |
|||
} |
|||
/* |
|||
wiki Curve_orientation |
|||
[http://mathoverflow.net/questions/44096/detecting-whether-directed-cycle-is-clockwise-or-counterclockwise] |
|||
The orientation of a triangle (clockwise/counterclockwise) is the sign of the determinant |
|||
matrix = { {1 , x1, y1}, {1 ,x2, y2} , {1, x3, y3}} |
|||
where |
|||
(x_1,y_1), (x_2,y_2), (x_3,y_3)$ |
|||
are the Cartesian coordinates of the three vertices of the triangle. |
|||
:<math>\mathbf{O} = \begin{bmatrix} |
|||
1 & x_{A} & y_{A} \\ |
|||
1 & x_{B} & y_{B} \\ |
|||
1 & x_{C} & y_{C}\end{bmatrix}.</math> |
|||
A formula for its determinant may be obtained, e.g., using the method of [[cofactor expansion]]: |
|||
:<math>\begin{align} |
|||
\det(O) &= 1\begin{vmatrix}x_{B}&y_{B}\\x_{C}&y_{C}\end{vmatrix} |
|||
-x_{A}\begin{vmatrix}1&y_{B}\\1&y_{C}\end{vmatrix} |
|||
+y_{A}\begin{vmatrix}1&x_{B}\\1&x_{C}\end{vmatrix} \\ |
|||
&= x_{B}y_{C}-y_{B}x_{C}-x_{A}y_{C}+x_{A}y_{B}+y_{A}x_{C}-y_{A}x_{B} \\ |
|||
&= (x_{B}y_{C}+x_{A}y_{B}+y_{A}x_{C})-(y_{A}x_{B}+y_{B}x_{C}+x_{A}y_{C}). |
|||
\end{align} |
|||
</math> |
|||
If the determinant is negative, then the polygon is oriented clockwise. If the determinant is positive, the polygon is oriented counterclockwise. The determinant is non-zero if points A, B, and C are non-[[collinear]]. In the above example, with points ordered A, B, C, etc., the determinant is negative, and therefore the polygon is clockwise. |
|||
*/ |
|||
double IsTriangleCounterclockwise(double xa, double ya, double xb, double yb, double xc, double yc) |
|||
{return ((xb*yc + xa*yb +ya*xc) - (ya*xb +yb*xc + xa*yc)); } |
|||
int DescribeTriangle(double xa, double ya, double xb, double yb, double xc, double yc) |
|||
{ |
|||
double t = IsTriangleCounterclockwise( xa, ya, xb, yb, xc, yc); |
|||
double a = GiveTriangleArea( xa, ya, xb, yb, xc, yc); |
|||
if (t>0) printf("this triangle is oriented counterclockwise, determinent = %f ; area = %f\n", t,a); |
|||
if (t<0) printf("this triangle is oriented clockwise, determinent = %f; area = %f\n", t,a); |
|||
if (t==0) printf("this triangle is degenerate: colinear or identical points, determinent = %f; area = %f\n", t,a); |
|||
return 0; |
|||
} |
|||
int main() |
|||
{ |
|||
// clockwise oriented triangles |
|||
DescribeTriangle(-94, 0, 92, 68, 400, 180); // https://www-sop.inria.fr/prisme/fiches/Arithmetique/index.html.en |
|||
DescribeTriangle(4.0, 1.0, 0.0, 9.0, 8.0, 3.0); // clockwise orientation https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex5.txt |
|||
// counterclockwise oriented triangles |
|||
DescribeTriangle(-50.00, 0.00, 50.00, 0.00, 0.00, 0.02); // a "cap" triangle. This example has an area of 1. |
|||
DescribeTriangle(0.0, 0.0, 3.0, 0.0, 0.0, 4.0); // a right triangle. This example has an area of (?? 3 ??) = 6 |
|||
DescribeTriangle(4.0, 1.0, 8.0, 3.0, 0.0, 9.0); // https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex1.txt |
|||
DescribeTriangle(-0.5, 0.0, 0.5, 0.0, 0.0, 0.866025403784439); // an equilateral triangle. This triangle has an area of sqrt(3)/4. |
|||
// degenerate triangles |
|||
DescribeTriangle(1.0, 0.0, 2.0, 2.0, 3.0, 4.0); // This triangle is degenerate: 3 colinear points. https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex6.txt |
|||
DescribeTriangle(4.0, 1.0, 0.0, 9.0, 4.0, 1.0); //2 identical points |
|||
DescribeTriangle(2.0, 3.0, 2.0, 3.0, 2.0, 3.0); // 3 identical points |
|||
return 0; |
|||
} |
|||
</source> |
|||
==Testing if point is inside polygon == |
==Testing if point is inside polygon == |
||
* stackoverflow<ref>[http://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon stackoverflow questions : How can I determine whether a 2D Point is within a Polygon?]</ref> |
* stackoverflow<ref>[http://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon stackoverflow questions : How can I determine whether a 2D Point is within a Polygon?]</ref> |
Revision as of 21:38, 25 December 2015
List:[1]
How to tell whether a point is to the right or left side of a line ?
/*
How to tell whether a point is to the right or left side of a line ?
http://stackoverflow.com/questions/1560492/how-to-tell-whether-a-point-is-to-the-right-or-left-side-of-a-line
a, b = points
line = ab
pont to check = z
position = sign((Bx - Ax) * (Y - Ay) - (By - Ay) * (X - Ax))
It is 0 on the line, and +1 on one side, -1 on the other side.
*/
double CheckSide(double Zx, double Zy, double Ax, double Ay, double Bx, double By)
{
return ((Bx - Ax) * (Zy - Ay) - (By - Ay) * (Zx - Ax));
}
Testing if point is inside triangle
/*
c console program
gcc t.c -Wall
./a.out
*/
# include <stdio.h>
// 3 points define triangle
double Zax = -0.250000000000000;
double Zay = 0.433012701892219;
// left when y
double Zlx = -0.112538773749444;
double Zly = 0.436719687479814 ;
double Zrx = -0.335875821657728;
double Zry = 0.316782798339332;
// points to test
// = inside triangle
double Zx = -0.209881783739630;
double Zy = +0.4;
// outside triangle
double Zxo = -0.193503885412548 ;
double Zyo = 0.521747636163664;
double Zxo2 = -0.338750000000000;
double Zyo2 = +0.440690927838329;
// ============ http://stackoverflow.com/questions/2049582/how-to-determine-a-point-in-a-2d-triangle
// In general, the simplest (and quite optimal) algorithm is checking on which side of the half-plane created by the edges the point is.
double sign (double x1, double y1, double x2, double y2, double x3, double y3)
{
return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3);
}
int PointInTriangle (double x, double y, double x1, double y1, double x2, double y2, double x3, double y3)
{
double b1, b2, b3;
b1 = sign(x, y, x1, y1, x2, y2) < 0.0;
b2 = sign(x, y, x2, y2, x3, y3) < 0.0;
b3 = sign(x, y, x3, y3, x1, y1) < 0.0;
return ((b1 == b2) && (b2 == b3));
}
int Describe_Position(double Zx, double Zy){
if (PointInTriangle( Zx, Zy, Zax, Zay, Zlx, Zly, Zrx, Zry))
printf(" Z is inside \n");
else printf(" Z is outside \n");
return 0;
}
// ======================================
int main(void){
Describe_Position(Zx, Zy);
Describe_Position(Zxo, Zyo);
Describe_Position(Zxo2, Zyo2);
return 0;
}
Orientation and area of the triangle
Orientation and area of the triangle : how to do it ?
// gcc t.c -Wall
// ./a.out
# include <stdio.h>
// http://ncalculators.com/geometry/triangle-area-by-3-points.htm
double GiveTriangleArea(double xa, double ya, double xb, double yb, double xc, double yc)
{
return ((xb*ya-xa*yb)+(xc*yb-xb*yc)+(xa*yc-xc*ya))/2.0;
}
/*
wiki Curve_orientation
[http://mathoverflow.net/questions/44096/detecting-whether-directed-cycle-is-clockwise-or-counterclockwise]
The orientation of a triangle (clockwise/counterclockwise) is the sign of the determinant
matrix = { {1 , x1, y1}, {1 ,x2, y2} , {1, x3, y3}}
where
(x_1,y_1), (x_2,y_2), (x_3,y_3)$
are the Cartesian coordinates of the three vertices of the triangle.
:<math>\mathbf{O} = \begin{bmatrix}
1 & x_{A} & y_{A} \\
1 & x_{B} & y_{B} \\
1 & x_{C} & y_{C}\end{bmatrix}.</math>
A formula for its determinant may be obtained, e.g., using the method of [[cofactor expansion]]:
:<math>\begin{align}
\det(O) &= 1\begin{vmatrix}x_{B}&y_{B}\\x_{C}&y_{C}\end{vmatrix}
-x_{A}\begin{vmatrix}1&y_{B}\\1&y_{C}\end{vmatrix}
+y_{A}\begin{vmatrix}1&x_{B}\\1&x_{C}\end{vmatrix} \\
&= x_{B}y_{C}-y_{B}x_{C}-x_{A}y_{C}+x_{A}y_{B}+y_{A}x_{C}-y_{A}x_{B} \\
&= (x_{B}y_{C}+x_{A}y_{B}+y_{A}x_{C})-(y_{A}x_{B}+y_{B}x_{C}+x_{A}y_{C}).
\end{align}
</math>
If the determinant is negative, then the polygon is oriented clockwise. If the determinant is positive, the polygon is oriented counterclockwise. The determinant is non-zero if points A, B, and C are non-[[collinear]]. In the above example, with points ordered A, B, C, etc., the determinant is negative, and therefore the polygon is clockwise.
*/
double IsTriangleCounterclockwise(double xa, double ya, double xb, double yb, double xc, double yc)
{return ((xb*yc + xa*yb +ya*xc) - (ya*xb +yb*xc + xa*yc)); }
int DescribeTriangle(double xa, double ya, double xb, double yb, double xc, double yc)
{
double t = IsTriangleCounterclockwise( xa, ya, xb, yb, xc, yc);
double a = GiveTriangleArea( xa, ya, xb, yb, xc, yc);
if (t>0) printf("this triangle is oriented counterclockwise, determinent = %f ; area = %f\n", t,a);
if (t<0) printf("this triangle is oriented clockwise, determinent = %f; area = %f\n", t,a);
if (t==0) printf("this triangle is degenerate: colinear or identical points, determinent = %f; area = %f\n", t,a);
return 0;
}
int main()
{
// clockwise oriented triangles
DescribeTriangle(-94, 0, 92, 68, 400, 180); // https://www-sop.inria.fr/prisme/fiches/Arithmetique/index.html.en
DescribeTriangle(4.0, 1.0, 0.0, 9.0, 8.0, 3.0); // clockwise orientation https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex5.txt
// counterclockwise oriented triangles
DescribeTriangle(-50.00, 0.00, 50.00, 0.00, 0.00, 0.02); // a "cap" triangle. This example has an area of 1.
DescribeTriangle(0.0, 0.0, 3.0, 0.0, 0.0, 4.0); // a right triangle. This example has an area of (?? 3 ??) = 6
DescribeTriangle(4.0, 1.0, 8.0, 3.0, 0.0, 9.0); // https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex1.txt
DescribeTriangle(-0.5, 0.0, 0.5, 0.0, 0.0, 0.866025403784439); // an equilateral triangle. This triangle has an area of sqrt(3)/4.
// degenerate triangles
DescribeTriangle(1.0, 0.0, 2.0, 2.0, 3.0, 4.0); // This triangle is degenerate: 3 colinear points. https://people.sc.fsu.edu/~jburkardt/datasets/triangles/tex6.txt
DescribeTriangle(4.0, 1.0, 0.0, 9.0, 4.0, 1.0); //2 identical points
DescribeTriangle(2.0, 3.0, 2.0, 3.0, 2.0, 3.0); // 3 identical points
return 0;
}
Testing if point is inside polygon
- stackoverflow[7]
References
- ↑ Michael Abrash's Graphics Programming Black Book Special Edition
- ↑ The Beauty of Bresenham's Algorithm by Alois Zingl
- ↑ bresenhams-drawing-algorithms
- ↑ accurate-point-in-triangle-test by Cedric Jules
- ↑ stackoverflow question : how-to-determine-a-point-in-a-2d-triangle
- ↑ js code
- ↑ stackoverflow questions : How can I determine whether a 2D Point is within a Polygon?