0

Good day! I'm currently trying to create a small 3D engine using C++, an Arduino UNO as well as a Nokia 5110 display with a library. Now I have successfully drawn a cube using triangles, but I want to fill the triangles to add proper lighting. My method for drawing triangles is the following:

LCD5110 lcd(8,9,10,12,11);
void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
 lcd.drawLine(x1, y1, x2, y2);
 lcd.drawLine(x2, y2, x3, y3);
 lcd.drawLine(x3, y3, x1, y1);
}

My library hasn't got a triangle draw or fill method unfortunately. I have no idea how I could fill a triangle so I hope for help.

Thanks in advance...

NOTE: Please keep it simple.

NOTE NR.2: I have already asked this question at StackOverflow but I haven't got a good answer so I will try it again.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jan 3, 2020 at 13:18
4
  • It looks like nobody is answering... Commented Jan 3, 2020 at 15:55
  • That is most likely because there is no good answer. If your library doesn't provide a draw filled polygon method you have to draw it yourself. Ok, this is a standard algorithm in computer graphics but it's not easy to explain. E.g. have not enough time to think about how to explain it in a few words. But I can point you to an easy to understand algorithm that is called the "flood fill" algorithm. If I recall correctly there is a C++ implementation called FloodSpill. Commented Jan 3, 2020 at 16:07
  • I will take a look at FloodSpill. Commented Jan 3, 2020 at 16:13
  • As I said in the comments of you answer: If the given solution works for you and performance doesn't matter, your solution is much better than my proposal. But don't forget to add the } to the code below. As it might confuse other that want to use your solution. Commented Jan 3, 2020 at 16:19

1 Answer 1

0

After tinkering around for hours I have finally found a great way to fill triangles! Here we go:

for(int x = x1; x<=x2; x++) {
 for(int y = y1; y<=y2; y++) {
 lcd.drawLine(x3, y3, x, y);
 }
}

Great!

answered Jan 3, 2020 at 16:06
1
  • If you add the missing } it will work. But it's not very efficient. If performance doesn't matter, you are right: It's simple and it works. Commented Jan 3, 2020 at 16:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.