Results 1 to 7 of 7

Thread: Polygon clipping by a line

Threaded View

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Polygon clipping by a line

    (I wonder if this would be more appropriate for the game development codebank or the math forum).

    This is a subroutine for clipping a general (convex or concave) polygon by a (infinite) straight line defined by 2 points.

    The polygon is a closed contour with N vertices, such that points 1 and N are the same. A polygon UDT is used (see code below) which includes the coordinates of the vertices, the number of branches and pointers to the first point of each branch. Initially the polygon is supposed to have a single branch, but as a result of clipping it may be split into a number of individual closed contours.

    The UDT definitions follow below. The subroutine comes next in a separate post as it's too long to fit in one post. Two screen shots show a demo polygon just before and after the clipping operation. A demo project is also included.
    VB Code:
    1. 'Data type for the polygon vertices
    2. Private Type punkt
    3.     X As Single
    4.     Y As Single
    5. End Type
    6. 'Data type for the entire polygon
    7. Private Type Polygon
    8.     vertex() As punkt
    9.     n As Integer        'Total number of vertices (first and last are the same)
    10.     up() As Integer     'Pointers to the vertices where the polygons start:
    11.     'when the polygon is plotted, we must go to them with "pen up"
    12.     nups As Integer     'Number of sub-polygons (branches): initially 1
    13.     'but after the polygon has been clipped, new polygons may be created
    14. End Type
    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by krtxmrtz; Aug 21st, 2006 at 04:42 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width