I for one think it's perfect that we can run the dedi on X-less machines and still get the output.
I gave it a brief test on my headless openSUSE server and it worked like a charm, so it definitely doesn't need a running X server.
<?php
/** Casts a ray from the tested point onwards
* along the X axis to determine if the point
* is inside or outside the area.
* Odd number of intersections means the point is inside the area.
*/
bool Area::contains(const Point& p)
{
int pX = p.x();
int pY = p.y();
bool inside = false;
/* Start with the segment made of last vertex (j = SIZE - 1) and first vertex (i = 0) of the polygon */
for (unsigned int j = 0, i = _vertices.size() - 1; j < _vertices.size(); j++) {
if (pY > _vertices[i].y() != pY > _vertices[j].y()) { /* Can the segment cross the ray vertically? If it cannot, skip it. */
/* WARNING: Magic ahead.
* If anybody wonders why this works, imagine that the formula below is nothing but an inverse linear function equation X = K*Y + Q
* where K = dX / dY and Q is the X coordinate of the starting point of the segment (note that the segments are created backwards).
* By solving this equation and comparing the result to pX we can tell whether the segment is "ahead of" or "behind" the tested point on the X axis
* at a given pY coordinate. If it is behind, the cast ray does not intersect the segment.
* (It's better to grab a piece of paper and visualise the whole solution) */
float shift = ((float)(_vertices[i].x() - _vertices[j].x()) / (float)(_vertices[i].y() - _vertices[j].y())) * (pY - _vertices[j].y()) + _vertices[j].x();
if (pX < shift)
inside = !inside;
}
i = j;
}
return inside;
}
?>