Drawing a Square on Black Background: Troubleshooting

AI Thread Summary
The discussion centers around a coding issue related to drawing a square on a black background using the PPM format. The initial code attempts to create a green square but results in an unexpected output. Key points include the identification of loop boundaries, where the outer loop should iterate from 0 to 479 and the inner loop from 0 to 639, correcting the off-by-one error that caused extra iterations. Additionally, there was confusion regarding the drawing logic, particularly in ensuring that the correct segments of each line were colored appropriately. The final resolution involved removing equation signs from the loop conditions, which successfully generated the desired green square.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top