How do I draw a triangle Pixel by pixel? I'm trying to copy the flag of the Bahamas, but I can't figure the triangle out.

This site requires JavaScript, please enable it in your browser!

GreenfootImage bg = getBackground(); for(int x = ___; x<=___; x++) { for(int y = ___; y<= ___; y++) { bg.setColorAt(x, y, COLOR); } }

LucarioCodes wrote...

How do I draw a triangle Pixel by pixel? I'm trying to copy the flag of the Bahamas, but I can't figure the triangle out.
<< Code Omitted >>int h = bg.getHeight(); // height of image to place triangle on (base length of triangle) int w = (int)(Math.sqrt(3) * h / 2); // width of triangle with a vertical left side (altitude of triangle)

for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) {

if (Math.abs(y - h / 2) * w < (w - x) * h / 2)

I still don't understand it, how do I acculy make the triangle appear on screen?

LucarioCodes wrote...

I still don't understand it, how do I acculy make the triangle appear on screen?public MyWorld() { super(800, 400, 1); GreenfootImage bg = getBackground(); int h = bg.getHeight(); int w = (int)(Math.sqrt(3) * h / 2); // add striping here for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (Math.abs(y - h / 2) * w < (w - x) * h / 2) bg.setColorAt(x, y, Color.BLACK); } } } }

Oh, thanks, sorry, I'm new to greenfoot

You need to login to post a reply.