Use backtick code blocks for syntax highlighting
This commit is contained in:
parent
8ed9dea394
commit
f658bfbb40
203 changed files with 26546 additions and 25801 deletions
72
41-01.md
72
41-01.md
|
|
@ -102,45 +102,47 @@ test whether a polygon is appropriately monotone.
|
|||
|
||||
**LISTING 41.1 L41-1.C**
|
||||
|
||||
/* Returns 1 if polygon described by passed-in vertex list is monotone with
|
||||
respect to a vertical line, 0 otherwise. Doesn't matter if polygon is simple
|
||||
(non-self-intersecting) or not. Tested with Borland C++ in small model. */
|
||||
```c
|
||||
/* Returns 1 if polygon described by passed-in vertex list is monotone with
|
||||
respect to a vertical line, 0 otherwise. Doesn't matter if polygon is simple
|
||||
(non-self-intersecting) or not. Tested with Borland C++ in small model. */
|
||||
|
||||
#include "polygon.h"
|
||||
#include "polygon.h"
|
||||
|
||||
#define SIGNUM(a) ((a>0)?1:((a<0)?-1:0))
|
||||
#define SIGNUM(a) ((a>0)?1:((a<0)?-1:0))
|
||||
|
||||
int PolygonIsMonotoneVertical(struct PointListHeader * VertexList)
|
||||
{
|
||||
int i, Length, DeltaYSign, PreviousDeltaYSign;
|
||||
int NumYReversals = 0;
|
||||
struct Point *VertexPtr = VertexList->PointPtr;
|
||||
int PolygonIsMonotoneVertical(struct PointListHeader * VertexList)
|
||||
{
|
||||
int i, Length, DeltaYSign, PreviousDeltaYSign;
|
||||
int NumYReversals = 0;
|
||||
struct Point *VertexPtr = VertexList->PointPtr;
|
||||
|
||||
/* Three or fewer points can't make a non-vertical-monotone polygon */
|
||||
if ((Length=VertexList->Length) < 4) return(1);
|
||||
/* Three or fewer points can't make a non-vertical-monotone polygon */
|
||||
if ((Length=VertexList->Length) < 4) return(1);
|
||||
|
||||
/* Scan to the first non-horizontal edge */
|
||||
PreviousDeltaYSign = SIGNUM(VertexPtr[Length-1].Y - VertexPtr[0].Y);
|
||||
i = 0;
|
||||
while ((PreviousDeltaYSign == 0) && (i < (Length-1))) {
|
||||
PreviousDeltaYSign = SIGNUM(VertexPtr[i].Y - VertexPtr[i+1].Y);
|
||||
i++;
|
||||
}
|
||||
/* Scan to the first non-horizontal edge */
|
||||
PreviousDeltaYSign = SIGNUM(VertexPtr[Length-1].Y - VertexPtr[0].Y);
|
||||
i = 0;
|
||||
while ((PreviousDeltaYSign == 0) && (i < (Length-1))) {
|
||||
PreviousDeltaYSign = SIGNUM(VertexPtr[i].Y - VertexPtr[i+1].Y);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i == (Length-1)) return(1); /* polygon is a flat line */
|
||||
if (i == (Length-1)) return(1); /* polygon is a flat line */
|
||||
|
||||
/* Now count Y reversals. Might miss one reversal, at the last vertex, but
|
||||
because reversal counts must be even, being off by one isn't a problem */
|
||||
do {
|
||||
if ((DeltaYSign = SIGNUM(VertexPtr[i].Y - VertexPtr[i+1].Y))
|
||||
!= 0) {
|
||||
if (DeltaYSign != PreviousDeltaYSign) {
|
||||
/* Switched Y direction; not vertical-monotone if
|
||||
reversed Y direction as many as three times */
|
||||
if (++NumYReversals > 2) return(0);
|
||||
PreviousDeltaYSign = DeltaYSign;
|
||||
}
|
||||
}
|
||||
} while (i++ < (Length-1));
|
||||
return(1); /* it's a vertical-monotone polygon */
|
||||
}
|
||||
/* Now count Y reversals. Might miss one reversal, at the last vertex, but
|
||||
because reversal counts must be even, being off by one isn't a problem */
|
||||
do {
|
||||
if ((DeltaYSign = SIGNUM(VertexPtr[i].Y - VertexPtr[i+1].Y))
|
||||
!= 0) {
|
||||
if (DeltaYSign != PreviousDeltaYSign) {
|
||||
/* Switched Y direction; not vertical-monotone if
|
||||
reversed Y direction as many as three times */
|
||||
if (++NumYReversals > 2) return(0);
|
||||
PreviousDeltaYSign = DeltaYSign;
|
||||
}
|
||||
}
|
||||
} while (i++ < (Length-1));
|
||||
return(1); /* it's a vertical-monotone polygon */
|
||||
}
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue