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.


1 2 3 4 5 6 7 8 | GreenfootImage bg = getBackground(); for ( int x = ___; x<=___; x++) { for ( int y = ___; y<= ___; y++) { bg.setColorAt(x, y, COLOR); } } |
1 2 | 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) |
1 2 3 4 | for ( int y = 0 ; y < h; y++) { for ( int x = 0 ; x < w; x++) { |
1 | if (Math.abs(y - h / 2 ) * w < (w - x) * h / 2 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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); } } } } |