What you need, is normals!
If you can make the white outlined version, then you're halfway there.
To make the red version, what you need to do is calculate the 2D normal of the two lines adjacent to that point.
How do you do that?
I'm glad you asked!
This code will calculate a normal for a line which points perpendicular to the line in 2D space.
If you go clockwise around your shape, you will get a bunch of normals which point outwards. So either go counterclockwise, or invert the order you pass the points to the function to get normals which point inwards.
Once you've done that, multiply the components of the normals for the two lines adjacent to each vertex. Ie, Nx# = N1x#*N2x#, Ny# = N1y#*N2y#
Then calculate the length of that normal, and normalize it just like I did in the code above with the normal I calculated for the line.
Once you've done that, you now have a vector which tells you which way you need to move that particular vertex. Decide how far you want to move it, multiply the components of the normal by that amount, and then add the normal's X and Y to the X and Y of the vertex. This is your new position for the vertex.
And voila, you have space between the polygons!
This is really just an inverse 2D version of what you do when you want to create an outline around a 3D mesh. We're just using 2D normals instead of 3D normals, and calculating the vertex normals with the normals of lines instead of the normals of planes, and we're moving the vertices inward instead of outward.
If you can make the white outlined version, then you're halfway there.
To make the red version, what you need to do is calculate the 2D normal of the two lines adjacent to that point.
How do you do that?
I'm glad you asked!
; ----------------------------------------------------------------------------------------------------------------------------------- ; This function calculates the equation of a 2D line. ; ; The equation of a 2D line is the same as the equation of a plane, except that the C component (Normal Z) is assumed to be 0. ; If facing down the line from point 1 to point 2, the line's normal will point left. ; In other words, if the line is an edge of a triangle wound clockwise, the resulting edge normals will point outwards. ; ; Function assumes positive Y is up, unlike screen coordinates. ; ----------------------------------------------------------------------------------------------------------------------------------- Function CLIP_Calculate2DLine(X1#, Y1#, X2#, Y2#) Local Nx#, Ny#, Nl# ; Calculate the normal of the line, (A = Nx, B = Ny) and the distance of the line (D) from the origin (0,0,0). Nx# = Y2# - Y1# Ny# = X1# - X2# ; Normalize the normal. Nl# = Sqr(Nx#*Nx# + Ny#*Ny#) CLIP_A# = Nx# / Nl# CLIP_B# = Ny# / Nl# CLIP_D# = -(CLIP_A#*X1# + CLIP_B#*Y1#) End Function
This code will calculate a normal for a line which points perpendicular to the line in 2D space.
If you go clockwise around your shape, you will get a bunch of normals which point outwards. So either go counterclockwise, or invert the order you pass the points to the function to get normals which point inwards.
Once you've done that, multiply the components of the normals for the two lines adjacent to each vertex. Ie, Nx# = N1x#*N2x#, Ny# = N1y#*N2y#
Then calculate the length of that normal, and normalize it just like I did in the code above with the normal I calculated for the line.
Once you've done that, you now have a vector which tells you which way you need to move that particular vertex. Decide how far you want to move it, multiply the components of the normal by that amount, and then add the normal's X and Y to the X and Y of the vertex. This is your new position for the vertex.
And voila, you have space between the polygons!
This is really just an inverse 2D version of what you do when you want to create an outline around a 3D mesh. We're just using 2D normals instead of 3D normals, and calculating the vertex normals with the normals of lines instead of the normals of planes, and we're moving the vertices inward instead of outward.