Drawing a Square on Black Background: Troubleshooting

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a coding issue related to drawing a square on a black background using the PPM format. Participants analyze the provided code and suggest modifications to achieve the desired output.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and describes the unexpected output when attempting to draw a square.
  • Another participant points out a potential issue with the placement of the newline character in the code.
  • A third participant questions the definitions of the strings used for colors and suggests that the loops may be iterating one time too many, proposing adjustments to the loop conditions.
  • This participant also notes that the logic for drawing the square may not be correctly implemented, particularly regarding the order of colors in the drawing process.
  • A later reply indicates that the original poster resolved the issue by removing equation signs from the loop conditions, resulting in the correct output.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the loop conditions, but there are differing views on the overall logic of the drawing process and how the colors should be applied.

Contextual Notes

There are unresolved assumptions regarding the definitions of the color strings and the intended logic for drawing the square, which may affect the understanding of the code's functionality.

Who May Find This Useful

Individuals interested in programming graphics, particularly those working with PPM format or troubleshooting similar coding issues.

static
Messages
3
Reaction score
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

Why?
 
Last edited by a moderator:
Technology news on Phys.org
Hrm. Your indentation didn't translate faithfully, but I do notice that the line where you print the newline is somewhat suspicious...
 
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.
 
thanks Mark44, it works!
I just deleted equation signs from outer and inner loop and beautiful green square appeared.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
4K
Replies
1
Views
4K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 2 ·
Replies
2
Views
622
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K