Clearing graphics after 20 reps?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 368 Views - Last Post: 20 February 2013 - 04:23 PM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Clearing graphics after 20 reps?

Posted 19 February 2013 - 03:51 PM

So here is another question :P. I have this code in a header file to be called on by a main() function:
void drawCircle(){ //Changed name so it was not a main() function. This way it can be put in a header and be called on by a main() function.

	int counter = 0.1, numx, numy; 
	float circ, sample;
	//got rid of 'double pi' because PI is already defined as a global variable.
	printf("How many circles do you need?\n");
	scanf("%s", &counter); //Add ampersand (&) sign before counter.

	//While must be lowercase w to get 'while.'
	while(counter > 0){ //changed word 'zero' to the number '0.'
		numx = rand() % 640 + 0;
		numy = rand() % 480 + 0;//These two random functions allow the circles to be drawn all over the screen instead of in just one spot.
		circ = twoPI(pi) * sampleNormal();
		circle(numx, numy, sampleNormal());//the 'n' on normal had to be capitalized. 
		printf("Circumference of circle I last drew was, %f", circ);//Added a ';' after parenthesis to end the line. Capitalize 'n' on normal again and add '()' after sampleNormal.
		counter++;//Got rid of space between 'counter' and '++.'
		for(int i = 0; i < 999999; i++){} //Added this 4 loop so the animation function will count to 1 million, which will slow it down a lot to be easily seen.
}//while ends
}//main ends, Don't use return 0 when you have (int* argc, char** argv) in main.

double sampleNormal() {
	double u = ((double) rand() / (RAND_MAX)) * 2 - 1;
	double v = ((double) rand() / (RAND_MAX)) * 2 - 1;
	double r = (u * u) + (v * v);
	double c = sqrt(-2 * log(r) / r); //Moved 'c' declaration up to other declared variables.
	if (r == 0 || r > 1) 
		return sampleNormal();
	else //Added an else statement
		return u * c;
}//sampleNormal ends

double twoPI(double x){return pow(x, 2);}//twoPI ends
//I added an argument to twoPI so it could take a number. Changed pi's to the variable 'x' so PI could be called by twoPI in main() function.


What this does is creates unphysically correct ripples. Lol instead of ripples going outward like they would in water it just makes the ripples appear randomly any old place. But this is ok. My issue is making the circles being drawn disappear after 20 repetitions of the code. I did get the program to clear the circles as soon as they came up, which means all you could see were quick flashes of white :P. But this is the closest I have gotten. I do not know how to tell it to begin erasing the circles after a certain amount of repetitions, and on top of that begin erasing them one at a time instead of all at once! haha

I don't think you can actually run this code since it requires a special program to be downloaded... But could anyone help with steering me towards ideas that SHOULD work?

Is This A Good Question/Topic? 0
  • +

Replies To: Clearing graphics after 20 reps?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 04:32 PM

What's wrong with a simple for loop that counts from 1 to 20?
Was This Post Helpful? 0
  • +
  • -

#3 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 06:58 PM

Lol for loop is not working :(. Nothing is working! No matter where I implement a for loop that counts up to 20, the program just runs like normal (not deleting old circles) or it clears circles as soon as they are drawn.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 07:18 PM

Show us your code where the for loop is not working. For that matter show us the code that clears the screen, I'm not seeing anything in the code that you posted that clears the screen.
Was This Post Helpful? 0
  • +
  • -

#5 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 07:37 PM

void drawCircle(){ //Changed name so it was not a main() function. This way it can be put in a header and be called on by a main() function.

	int counter = 0.1, numx, numy, rep; 
	float circ, sample;
	//got rid of 'double pi' because PI is already defined as a global variable.
	printf("How many circles do you need?\n");
	scanf("%s", &counter); //Add ampersand (&) sign before counter.
		//While must be lowercase w to get 'while.'
		while(counter > 0){ //changed word 'zero' to the number '0.'
			numx = rand() % 640 + 0;
			numy = rand() % 480 + 0;//These two random functions allow the circles to be drawn all over the screen instead of in just one spot.
			circ = twoPI(pi) * sampleNormal();
			circle(numx, numy, sampleNormal());//the 'n' on normal had to be capitalized. 
			printf("Circumference of circle I last drew was, %f", circ);//Added a ';' after parenthesis to end the line. Capitalize 'n' on normal again and add '()' after sampleNormal.
			counter++;//Got rid of space between 'counter' and '++.'
			for(int i = 0; i < 999999; i++){} //Added this 4 loop so the animation function will count to 1 million, which will slow it down a lot to be easily seen.
		[b]for(counter = 0.1; counter <= 20; counter++)
			cleardevice();[/b]
		}//while ends
}//main ends, Don't use return 0 when you have (int* argc, char** argv) in main.


This is the code that draws circles on the graphics screen but clears them immediately. I tried throwing the for loop in other places around the while loop but it just ended up acting like it wasn't even there (just drew circles all over and never cleared them). (I bolded the for loop added)

The original code I posted was before I messed with anything haha. That one just draws circles all over the graphics screen without clearing any of them.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 07:47 PM

Your line 17-18 there is clearing the device 21 times.
Was This Post Helpful? 0
  • +
  • -

#7 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 07:50 PM

So do I not want a for loop and just a cleardevice at the end of the while loop?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 07:56 PM

No. If your objective is to draw 20 circles, and then clear the screen, then loop 20 times using a for loop (or a while loop), and then clear the screen. Within that loop draw your circle.

Consider this code:
for(int i = 0; i < 5; i++)
{
    printf("x\n");
}
printf("done.\n");


It loops 5 times printing 'x', and then prints done. So you'll want to do something similar where you'll draw a random circle 20 times, and then clear the screen.
Was This Post Helpful? 0
  • +
  • -

#9 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 09:29 PM

void drawCircle(){ //Changed name so it was not a main() function. This way it can be put in a header and be called on by a main() function.

	int counter = 0.1, numx, numy, rep; 
	float circ, sample;
	//got rid of 'double pi' because PI is already defined as a global variable.
	printf("How many circles do you need?\n");
	scanf("%s", &counter); //Add ampersand (&) sign before counter.
	
	

		//While must be lowercase w to get 'while.'
	while(counter > 0){ //changed word 'zero' to the number '0.'
		for(int i = 0; i < 999999; i++){} //Added this 4 loop so the animation function will count to 1 million, which will slow it down a lot to be easily seen.
		circ = twoPI(pi) * sampleNormal();
		for(int counter = 0.1; counter < 20; counter++){
			numx = rand() % 640 + 0;
			numy = rand() % 480 + 0;//These two random functions allow the circles to be drawn all over the screen instead of in just one spot.
			circle(numx, numy, sampleNormal());//the 'n' on normal had to be capitalized. 
			printf("Circumference of circle I last drew was, %f", circ);//Added a ';' after parenthesis to end the line. Capitalize 'n' on normal again and add '()' after sampleNormal.
			//counter++;//Got rid of space between 'counter' and '++.'
		}//while ends
		cleardevice();}
}//main ends, Don't use return 0 when you have (int* argc, char** argv) in main.



WOOHOO! kind of haha. I now have this code. This now displays a good handful of circles at a time, however they are now going sooo fast you cannot even see them (except for white little flashes). My count to 1 million loop is apparently no longer working... One problem down, another arises...
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 10:09 PM

Quote

So here is another question :P. I have this code in a header file to be called on by a main() function:

You should not have "code" in a header file. It should be in another C++ file that gets compiled with your main.cpp file. You use include file to prototype functions. You include .h files and add /.cpp files to your project. But usually including source files is considered a bad practice.


Jim
Was This Post Helpful? 0
  • +
  • -

#11 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 10:15 PM

It is a homemade header. We are supposed to add functions to it (thus adding code) and then call those functions from a main.

OOH! But I solved my too fast problem. I now have a code that draws random circles in random places, only draws a good handful before wiping, and now is slow enough to see each circle before it wipes. THAT'S EVERYTHING!

I just used Sleep(10) in order to get it to calm down... haha. Yeah... it took a lot of sleeps...

This post has been edited by Bawnawgwa: 19 February 2013 - 10:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Clearing graphics after 20 reps?

Posted 19 February 2013 - 11:24 PM

Quote

It is a homemade header. We are supposed to add functions to it (thus adding code) and then call those functions from a main.

I don't care if the header is homemade or made by some true expert in C/C++, having code in a header file is normally a very bad practice. Source should be in source files, not headers.

Jim

This post has been edited by jimblumberg: 19 February 2013 - 11:25 PM

Was This Post Helpful? 2
  • +
  • -

#13 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Clearing graphics after 20 reps?

Posted 20 February 2013 - 10:43 AM

View Postjimblumberg, on 19 February 2013 - 11:24 PM, said:

Quote

It is a homemade header. We are supposed to add functions to it (thus adding code) and then call those functions from a main.

I don't care if the header is homemade or made by some true expert in C/C++, having code in a header file is normally a very bad practice. Source should be in source files, not headers.

Jim


Umm... what you say makes no sense? What is in a header file... CODE! That is how it operates. A header file is full of functions (i.e. CODE!) that you can #include and call from a main() function. That is what we are doing!!! We are adding functions to a header file to call from a main() function!

And it matters not what is "good practice" at this point. My professor wants us to add functions to his header file, so that is what we are doing!
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: Clearing graphics after 20 reps?

Posted 20 February 2013 - 10:50 AM

If you insist on having code in your header files, I challenge you to make the following compile and link without using any macro hacks. There are 3 files: common.h, main.c, and parttwo.c. Common.h incorrectly has the code in the header.

common.h:
#include <stdio.h>

void HelloWorld()
{
    printf("Hello, World\n");
}



main.c:
#include "common.h"

extern void PartTwo();

int main()
{
    HelloWorld();
    PartTwo();
}



parttwo.c:
#include "common.h"

int PartTwo()
{
    HelloWorld();
}


Was This Post Helpful? 1
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Clearing graphics after 20 reps?

Posted 20 February 2013 - 10:55 AM

Quote

Umm... what you say makes no sense? What is in a header file... CODE!

No, header files usually contain just function definitions (prototypes), structure or class definitions, other definitions, not complete function implementations. Unless you are using C++ template functions, or inline class member functions header files should not contain function implementations. Including files that have function implementations can often times lead to hard to find bugs, because of how the #include mechanism works.

You should only be adding the prototypes of your functions to this header file not the implementations.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2