2
\$\begingroup\$

I'm rendering equilateral triangles of different sizes and I'm having trouble making them symmetric along the vertical axis. Here I have rendered two triangles. The triangle on the left (rendered correctly) has the coordinates (0.0, 0.0), (-7.0, 14.0) and (7.0, 14.0). The triangle on the right, on the other hand, has the coordinates (0.0, 0.0), (-7.0, 15.0) and (7.0, 15.0) and is not symmetric along the vertical axis.

Triangles

Here are the matrices I'm using:

glm::mat4 model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(90.0f, 90.0f, 0.0f));
glm::mat4 view_matrix = glm::mat4(1.0f);
glm::mat4 projection_matrix = glm::ortho(0.0f, w * 1.0f, h * 1.0f, 0.0f);
glm::mat4 mvp_matrix = projection_matrix * view_matrix * model_matrix;

I'm aware of the diamond exit rule but I don't think that's the cause of the problem. Seems like the diamond exit rule only applies to line segments? This problem also exists when I try to make triangles symmetric in the horizontal axis. Cheers!

\$\endgroup\$
  • \$\begingroup\$ Can you try adding half a pixel to their coordinates and let us know what that does? \$\endgroup\$ – mouseas Apr 25 '14 at 22:51
  • \$\begingroup\$ I have tried that and it sometimes fixes the issue but not always. \$\endgroup\$ – Izaan Apr 25 '14 at 23:12
  • \$\begingroup\$ Can you speak more to your application for this? Neither of the triangles you've sketched out here is equilateral. Do you perhaps mean isosceles (mathworld.wolfram.com/IsoscelesTriangle.html )? \$\endgroup\$ – Steven Stadnicki Apr 26 '14 at 1:06
  • \$\begingroup\$ Sorry, the triangles were meant to be equilateral but I ended up making them isosceles while trying to make them symmetrical. The application is just me experimenting with things, I should have clarified this. \$\endgroup\$ – Izaan Apr 26 '14 at 11:36
3
\$\begingroup\$

This is a notoriously hard problem - the short answer is that to have all the right properties when it comes to overlapping line segments, line drawing has to have some inherent asymmetries; for instance, a line that falls on a half-pixel boundary must be drawn on that pixel can only be drawn on that half-pixel if the line comes 'from the left' but not 'from the right', in a loose sense. If you need your triangles to be perfectly symmetric, you're best off explicitly mirroring the pixels yourself.

\$\endgroup\$
  • \$\begingroup\$ Thank you, that makes sense. Is there somewhere I can learn more about this? I'm guessing I should look for rasterization rules. \$\endgroup\$ – Izaan Apr 26 '14 at 11:37
  • 1
    \$\begingroup\$ @IzaanSiddiqi That's a good starting point; you'll also want to learn about line-drawing algorithms, in particular Bresenham's. If you're only drawing isolated triangles then the mirroring will work well, but note that even something as simple as trying to draw a strip of alternating point-up and point-down triangles will yield triangles of different shapes unless you're very careful - your left triangle, for instance, is left-right symmetric but its edges aren't top-bottom symmetric. \$\endgroup\$ – Steven Stadnicki Apr 26 '14 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.