I am using JavaScript to create a shape using clip-path. I have no issues with the code but my issue lies with the math. I can get close to my goal but it fails once the shape's height or width is manipulated. I want to create a shape like this simple polygon.
My problem is that I do not know how to calculate where to place points within this rectangular polygon that will result in a copy of the polygon within itself, but with each of the interior polygon's edges being x units away from the surrounding polygon. The resulting inner polygon needs to maintain this distance when the outer polygon changes height or width. Using the simple polygon image as an example the outer edges of the image are the outer polygon, and the inner polygon is removing the image creating a hole the shape of the outer polygon.
In the image, and the context I am graphing this polygon, the top left is (0,0). The points are also color coded to match the coordinates bellow which are percent based in this case and separated by a single space with commas separating each point. For example the first listed coordinate at the bottom (dark orange) is the coordinate for the upper most left point. For this point x = 25% = polygon height * 0.25 and y = 0. (non-percentages are allowed)
My issue is once the shape changes height, width, or thickness it seems to fail as seen by my tests here. Thickness is how many units smaller the inner polygon should be. These are my best results so far as the inner points always seem to be off to some degree.
The first shape is 100x100 pixels with a thickness of 5 pixels. The second is 100x50, and the last is 100x10.
My thinking is to use these triangles in the polygon, get the points on the hypotenuse that are closest to the desired location, then attempt to shift it inward however many units equal to the thickness variable.
I have these variables:
height = polygon height, width = polygon width
h = height (triangle rise), w = width * 0.25 (triangle run), thickness = 5,
angle = atan(h/w),
hypotenuse = sqrt(h^2 + w^2)
pX1 = thickness * cos(angle), pY1 = thickness * sin(angle)
pX2 = hypotenuse * cos(angle), pY2 = hypotenuse * sin(angle)
Back to this image, here are the points I am trying to get:
dark blue = point1; dark pink = point2; grey = point3; cyan = point4
point1 = (pX2 + thickness, height - pY2 + thickness)
point2 = (width - pX1 - thickness, pY1)
point3 = ( (width * 0.5) + pX2 - thickness, pY2 - thickness )
point4 = (pX1, height - pY1)
This was mostly just a bunch of trial and error + research into right triangles so apologies if the math is messy. I am also weak in trigonometry. I also translated this all from JavaScript so let me know if any more clarification is needed.
Here you can see a live view of the math in action. (If you're not familiar with web dev just focus on the JS section at the top right, that's where all the math is)

