Skip to content

Commit ed36574

Browse files
committed
Explain why (I think that) vp_cedges is being calculated correctly
1 parent f9e81a4 commit ed36574

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

kos/src/libvideo/gfx/polygon.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <hybrid/compiler.h>
2929

3030
#include <hybrid/bitset.h>
31-
#include <hybrid/sequence/bsearch.h>
3231

3332
#include <kos/anno.h>
3433

@@ -301,6 +300,44 @@ libvideo_generic_polygon_create(struct video_domain const *__restrict self,
301300
* still only has 4 edges, which is the result of the # of edges of
302301
* the original polygon (2), plus the max. # of edges in any vertical
303302
* scanline (which is also 2).
303+
*
304+
*
305+
*
306+
* NOTE: I don't actually have a proof for this assumption:
307+
* >> The max # of edges of any axis-aligned rect-clipped polygon is
308+
* >> NUM_EDGES_OF_ORIG_POLYGON +
309+
* >> MAX(foreach(X) NUM_INTERSECTING_EDGES_OF_VERTICAL_SCANLINE_AT(X)) +
310+
* >> MAX(foreach(Y) NUM_INTERSECTING_EDGES_OF_HORIZONTAL_SCANLINE_AT(X));
311+
*
312+
* (This assumption being the basis for how we assign "vp_cedges" here, with
313+
* the only deviation from the above assumption being that we omit the
314+
* "MAX(foreach(Y) NUM_INTERSECTING_EDGES_OF_HORIZONTAL_SCANLINE_AT(X))" part
315+
* because don't need to track perfectly horizontal edges)
316+
*
317+
* However, despite not having a proof, I also have yet to find any polygon
318+
* that can be clipped in such a way that more edges are needed to represent
319+
* the result of clip than are needed by the above.
320+
*
321+
*
322+
*
323+
* Even more abstract, the above formula can be simplified as:
324+
* >> The max # of edges to represent a line-L-clipped polygon is
325+
* >> NUM_EDGES_OF_ORIG_POLYGON +
326+
* >> (MAX(foreach(D) NUM_INTERSCTING_EDGES_OF_LINE_ROT90(D)) / 2)
327+
* Where "NUM_INTERSCTING_EDGES_OF_LINE_ROT90" is the # of edges
328+
* that intersect with a diagonal going through the point P and
329+
* with an angle that is 90° offset from the original line L:
330+
* P = L.P0 + ((L.P1 - L.P0) * D) (Where D is REAL and in [0,1])
331+
*
332+
* Using this formula, and the fact that a rect has 4 lines (2 of each
333+
* of which are each identical, meaning the "/ 2" can be ignored), we
334+
* end up with the original formula for rects above.
335+
*
336+
*
337+
*
338+
* But again: the above formula is actually just a theory, since I came up
339+
* with it myself, and don't have any proof that it is actually correct.
340+
* It only *feels* correct, and I don't have any counter-example.
304341
*/
305342
result->vp_cedges = nedges + poly_xactive;
306343
freea(active);
@@ -442,7 +479,7 @@ libvideo_polygon_data_clip(struct video_polygon_data const *__restrict self,
442479
video_polygon_edge_getp0x(&edge));
443480
video_dim_t xoff = (video_dim_t)(video_rect_getxmin(rect) -
444481
video_polygon_edge_getp0x(&edge));
445-
video_coord_t yrel = (video_coord_t)(((__UINT_FAST64_TYPE__)xoff * ydim) / xdim);
482+
video_coord_t yrel = (video_coord_t)(((uint_fast64_t)xoff * ydim) / xdim);
446483
video_offset_t yhit = video_polygon_edge_getp0y(&edge) + yrel;
447484
struct video_polygon_edge vedge;
448485
vedge.vpe_dir = edge.vpe_dir;
@@ -466,7 +503,7 @@ libvideo_polygon_data_clip(struct video_polygon_data const *__restrict self,
466503
video_polygon_edge_getp1x(&edge));
467504
video_dim_t xoff = (video_dim_t)(video_rect_getxmin(rect) -
468505
video_polygon_edge_getp1x(&edge));
469-
video_coord_t yrel = (video_coord_t)(((__UINT_FAST64_TYPE__)xoff * ydim) / xdim);
506+
video_coord_t yrel = (video_coord_t)(((uint_fast64_t)xoff * ydim) / xdim);
470507
video_offset_t yhit = video_polygon_edge_getp1y(&edge) - yrel;
471508
struct video_polygon_edge vedge;
472509
vedge.vpe_dir = edge.vpe_dir;
@@ -501,7 +538,7 @@ libvideo_polygon_data_clip(struct video_polygon_data const *__restrict self,
501538
video_polygon_edge_getp1x(&edge));
502539
video_dim_t xoff = (video_dim_t)(video_rect_getxend(rect) -
503540
video_polygon_edge_getp1x(&edge));
504-
video_coord_t yrel = (video_coord_t)(((__UINT_FAST64_TYPE__)xoff * ydim) / xdim);
541+
video_coord_t yrel = (video_coord_t)(((uint_fast64_t)xoff * ydim) / xdim);
505542
video_offset_t yhit = video_polygon_edge_getp1y(&edge) - yrel;
506543
struct video_polygon_edge vedge;
507544
vedge.vpe_dir = edge.vpe_dir;
@@ -525,7 +562,7 @@ libvideo_polygon_data_clip(struct video_polygon_data const *__restrict self,
525562
video_polygon_edge_getp0x(&edge));
526563
video_dim_t xoff = (video_dim_t)(video_rect_getxend(rect) -
527564
video_polygon_edge_getp0x(&edge));
528-
video_coord_t yrel = (video_coord_t)(((__UINT_FAST64_TYPE__)xoff * ydim) / xdim);
565+
video_coord_t yrel = (video_coord_t)(((uint_fast64_t)xoff * ydim) / xdim);
529566
video_offset_t yhit = video_polygon_edge_getp0y(&edge) + yrel;
530567
struct video_polygon_edge vedge;
531568
vedge.vpe_dir = edge.vpe_dir;

0 commit comments

Comments
 (0)