Convert area found with Shoelace from pixels to cm

geometrypolygons

I am working on an app that finds areas of various polygons using the Shoelace theorem.
The x and y are in pixels but then the user can set one length in the drawing.
For example – a user can draw a square and says one segment is $4$ cm or $4$ in.
Now I need to transform my resulted area from pixels to cm.

Any idea how to do this pls?

I can easily transform the lengths of the segments – I find a rate between the segment the user defined – if the user says that the segment is 10 cm and the segment has $1000$ pixels, I use that scale for all the segments.

Do I need to apply this rate to the x and y before applying the formula? Or can I apply a rate at the end like with segments?

We have all vertices in pixels (very large numbers), from that I calculate all segment lengths and the area (using the Shoelace theorem)

This is an example of some coordinates

"coordinates":{
        "A":{
            "x":198,
            "y":80,
        },
        "B":{
            "x":361,
            "y":79,
        "C":{
            "x":357,
            "y":239,
        },
        "D":{
            "x":194,
            "y":238,
        }
    }

The segment lengths resulting (in pixels) are $158$ and the resulting area is approx $25 000$ pixels.
This is in pixels but the real lengths are $2$ cm for each side and the area should be $4$.

Best Answer

What might be confusing you is that you are using pixel both as a unit of length and as a unit of area. But really a pixel is a small square with side $\text{pixellength}$ and area $\text{pixellength}^2$.

If the conversion factor for the length of a segment is $r \frac{\text{cm}}{\text{pixellength}}$, then the conversion rate for the area is $r^2 \frac{\text{cm}^2}{\text{pixellength}^2}$ regardless of the shape. This can be applied afterwards. In your example $r=\frac{2}{158}$ so the area is $25000 \cdot \left(\frac{2}{158}\right)^2 = 4.0\ \ \ \text{cm}^2$.

You could of course instead apply the factor $r$ to all the $x$ and $y$ coordinates to convert them to centimeters, and then apply the shoelace formula to get the area but this should give the same answer. This is because every term in the formula has had two factors of $r$ applied to it, and these can be factored out and applied once at the end instead.