Error in drawRoundedRectHelperGetPoints?

Started by Nafffen, 21 December 2023, 11:32:40

Nafffen

Hey, I'm not sure about this, but I looked in your drawRoundedRectHelperGetPoints code to help myself with a project, and I think your computation for Bottom left corner and Bottom right corner is slighty offset.
Instead of:
// Bottom left corner
        for (unsigned int i = 0; i < nrCornerPoints; ++i)
        {
            points.emplace_back(offset + radius + (radius * std::cos(twoPi * (2*(nrCornerPoints - 1) + i) / nrPointsInCircle)),
                                offset + size.y - radius - (radius * std::sin(twoPi * (2*(nrCornerPoints - 1) + i) / nrPointsInCircle)));
        }

        // Bottom right corner
        for (unsigned int i = 0; i < nrCornerPoints; ++i)
        {
            points.emplace_back(offset + size.x - radius + (radius * std::cos(twoPi * (3*(nrCornerPoints - 1) + i) / nrPointsInCircle)),
                                offset + size.y - radius - (radius * std::sin(twoPi * (3*(nrCornerPoints - 1) + i) / nrPointsInCircle)));
        }
The following will give more accurate result:
// Bottom left corner
        for (unsigned int i = 0; i < nrCornerPoints; ++i)
        {
            points.emplace_back(offset + radius + (radius * std::cos(twoPi * ((2*nrCornerPoints - 1) + i) / nrPointsInCircle)),
                                offset + size.y - radius - (radius * std::sin(twoPi * ((2*nrCornerPoints - 1) + i) / nrPointsInCircle)));
        }

        // Bottom right corner
        for (unsigned int i = 0; i < nrCornerPoints; ++i)
        {
            points.emplace_back(offset + size.x - radius + (radius * std::cos(twoPi * ((3*nrCornerPoints - 1) + i) / nrPointsInCircle)),
                                offset + size.y - radius - (radius * std::sin(twoPi * ((3*nrCornerPoints - 1) + i) / nrPointsInCircle)));
        }

The problem was removing 1 before multiplied by 2 (or 3 for the next corner), we need to remove it after the multiplication.

Tell me if I am wrong.

texus

#1
My first instinct was to just render with both versions of the code and see which one looked correct, so that I didn't have to bother with understanding the math again, but I couldn't visually tell the difference between both variants :)

I think my code is correct though.

Let's examine the bottom left corner. The first point needs to be at the leftmost position (as it is connected by a vertical line from the last point of the top-left corner), while the last point needs to be at the bottom-most position (as it needs to be connected with a horizontal line to the first point of the bottom-right corner).
So when you plug in i=0 in the formula, you need to get a cos value of -1 and a sin value of 0. When you set i=nrCornerPoints-1 (the last iteration of the for loop), the cos value should be 0 and the sin value should be -1. I verified with WolframAlpha that this is the case when using my formula.

The thing that makes it weird is probably that when drawing a circle (instead of a rounded rectangle), the top, right, bottom and left points on the circle are duplicated. There is also the fact that the "nrPointsInCircle" might be a bad name. There are "4 * nrCornerPoints" points in total, but "nrPointsInCircle" is set to "4 * (nrCornerPoints - 1)" (the amount of unique points when drawing a circle instead of a rounded rectangle).

Nafffen

"4 * (nrCornerPoints - 1)"
Oooh that's where I was wrong, I didn't see this -1 my bad  ;D
Thanks for the reply