Drawing a Square on Black Background: Troubleshooting

In summary, the programmer attempted to use a for loop to draw a square on a black background, but the code did not work as intended and resulted in an image that looked like a test pattern.
  • #1
static
3
0
My task is to draw a square on black background using ppm.
What I did is this loop:
Code:
	for (int h=0; h<=480; h++){
		for (int w=0; w<=640; w++){

				if (h>100 && h<380 && w<460){
				//green paint
			fprintf (fp, "%s", greenColor); 
			fprintf (fp, "%s", space);
				} else {
				//paint in black
			fprintf (fp, "%s", blackColor);
			fprintf (fp, "%s", space);
fprintf (fp, "%s", "\n");
				}
				
			}
			
		
	
	}

but instead of getting a square I get sth like this:
http://www.polew.pl/file/wastage/test.gif [Broken]

Why?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hrm. Your indentation didn't translate faithfully, but I do notice that the line where you print the newline is somewhat suspicious...
 
  • #3
How are your strings greenColor, blackColor, and space defined? It looks to me like some of your lines are too long and are wrapping to the next screen line.

I'm assuming that you are drawing lines from the top of the screen down, and that the upper left corner of the screen is (0, 0) and the lower right corner is (639, 479). This means that both your loops are running one iteration too many. The outer loop should be
Code:
for (h = 0; h < 480 ; h++)
{
}
and your inner loop should be similarly adjusted
Code:
for (w = 0; h < 640 ; h++)
{
}
My outer loop runs 480 times; yours runs 481 times. My inner loop runs 640 times for each iteration of the outer loop; your runs 641 times for each iteration of the outer loop.

Those points aside, your code puzzles me. I would think you would want to draw some solid black lines for 100 screen lines, and then draw lines that are black for the first 100 pixels, then green for 280 pixels, then black again for the remainder of each line for lines 101 through 379. I don't see your code doing the first black part on each line that the rectangle is in.

After that, you want to draw all black lines for the remaining screen lines.
 
  • #4
thanks Mark44, it works!
I just deleted equation signs from outer and inner loop and beautiful green square appeared.
 

1. How do I create a perfect square on a black background?

To create a perfect square on a black background, you can use a ruler or a straight edge to draw the four sides of the square. You can also use a stencil or a square template to ensure that all the sides are equal in length.

2. Why does my square have jagged edges?

This could be due to using a dull pencil or an uneven surface. Make sure to use a sharp pencil and a smooth surface to draw the square. You can also try using a thicker pencil or a pen to create smoother edges.

3. How can I make my square more visually appealing on a black background?

To make your square stand out on a black background, you can use a white or metallic colored pencil to outline the edges of the square. You can also add shading or texture to the square to make it more interesting.

4. Why does my square appear distorted on the black background?

This could be due to an incorrect perspective or angle when drawing the square. Make sure to use a ruler or a straight edge to keep the sides of the square parallel and equal in length. You can also try drawing the square from a different angle or using a reference image for accuracy.

5. What can I do to fix mistakes when drawing a square on a black background?

If you make a mistake while drawing your square, you can use an eraser to correct it. However, if the mistake is too prominent, you can cover it up with a small piece of black paper or use a black marker to fill in the area around the square.

Similar threads

  • Programming and Computer Science
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
Replies
2
Views
4K
  • Programming and Computer Science
2
Replies
41
Views
4K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
20
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top