Page 1 of 3

Spirograph what I did from lesson 10

Posted: May 23rd, 2012, 12:04 am
by Asimov
Hi all,

After making a square and then getting the function to work for drawing a line, I had a go at something I did on graph paper at school.

I am not sure if I could have written it better, but I am rusty at C++.

Anyway just to show I am learning. Hope to carry on with the tutorials now.

I might do this again with randomizing the colour soon as well, just can't remember the code for randomizing yet. It is usually RND, but it is ages since I needed to use it.

Would be cool if I could making this spin, but then it would be back to maths again LOL. Probably something to do with cosine or something.

Code: Select all

void Game::star()
{
	int movey=20;
	for (int movex=400; movex < 650; movex=movex+10)
		{
			gfx.DrawLine(400,movey,movex,250,0,255,0);
			if (movey < 250) movey=movey+10;
		}
	// Hieght
	movey=500;
	//across
	for (int movex=400; movex < 650; movex=movex+10)
		{
			gfx.DrawLine(400,movey,movex,250,255,0,0);
			if (movey > 250) movey=movey-10;
		}
	movey=500;
	//across
	for (int movex=400; movex > 150; movex=movex-10)
		{
			gfx.DrawLine(400,movey,movex,250,255,0,0);
			if (movey > 250) movey=movey-10;
		}
	movey=20;
	for (int movex=400; movex > 150; movex=movex-10)
		{
			gfx.DrawLine(400,movey,movex,250,0,255,0);
			if (movey < 250) movey=movey+10;
		}
	

}


Re: Spirograph what I did from lesson 10

Posted: May 23rd, 2012, 11:08 am
by LuX
Looks cool.
for a random number, use rand() % *maximum value*, which will then generate a random value between 0 and your maximum value.

It's not 100% random however....

You can use a seed to randomize, which will always display the same value, like this:

srand(*seed as a number*);
int x = rand() % 100;

In this case, whatever you input as seed, it will display the same value each time you run the program at the same seed.

Re: Spirograph what I did from lesson 10

Posted: May 23rd, 2012, 2:13 pm
by chili
This is some funky shit bro, I like it. :)

It would be really easy to make it rotate too. Wanna see? ;)

Re: Spirograph what I did from lesson 10

Posted: May 23rd, 2012, 4:03 pm
by Asimov
Hi Chilli,

Yes I would like to see.

I am pretty good at working out problems, but my mathematics suffer.

I am guessing it is somehow translating the value of Pi to coordinates.

In my Dalek draughts game I think I used something called a quaternion, but that was in C# and XNA.
Just rotating my board took a lot of research.

@Lux: thanks for the heads up on the randomize. I switch between languages quite a lot and I somehow get my syntaxes mixed up. Now I can randomize my colours heh heh.

Asimov

Re: Spirograph what I did from lesson 10

Posted: May 24th, 2012, 12:18 pm
by chili
Rockin' spinnaz yo! :lol:

Re: Spirograph what I did from lesson 10

Posted: May 24th, 2012, 1:17 pm
by LuX
Hypnotizing... This is gonna be my new screensaver : -D

Just one question, like I couldn't google it, but I think its interesting if people write more stuff here:
Is there any difference in 0.001f and 0.001, if the variable is already defined to be a floating number?

Oh, and also a tip for PI:
Normally you can't access math.h's pi, but if you add this:

#define _USE_MATH_DEFINES
#include "math.h"

You can then use M_PI as an accurate pi

Re: Spirograph what I did from lesson 10

Posted: May 24th, 2012, 3:16 pm
by LuX
Here's my screensaver mod : -]
You start off with one star flying around, but you can clone it by clicking on it.
The stars will reduce size and split into two, you can click on 'em until you have small specs flying around.

It's still buggy but i'm not interested enough to make it better... Some problems are that sometimes you are only able to clone by clicking the "mother" star, sometimes the stars get stuck on the border, and usually cloning them will spawn them at a completely different location, but you get the idea.

The screen should automatically adjust to your screen size, and you can close it by pressing space.

Fun little thing. I used chili's version to make this...
Fix for the position glitch is btw at the bottom where it says sy = sx[n]; when it should be sy = sy[n];
It should also fix the bug where the stars get stuck at the bottom.

Re: Spirograph what I did from lesson 10

Posted: May 24th, 2012, 4:03 pm
by Asimov
Hi Chilli,

Thank you that is amazing.

I have been studying your code. I know you are using cosine and sine to produce the effect, but I am not sure how it works. The shape is created by four loops, so the centre of rotation must be set to the centre of the shape. I like to know how it works, not that it just works if you know what I mean.
I do wish I was better at maths geometry. I am going to play about with the code and see if I can work it out

I have just started on your circle tutorial. I like the way you described how pythagorus is used to work out how to draw the circle. I am going to have to re watch it a few times before I understand the theory. I am afraid I start with good intentions and I slowly get lost heh heh

I notice that you add the f to 1.000f when using rotations. I believe this is used to show the degree of rotation (I might have typed that number wrong, but the question is still the same heh heh).

I am going back to study your code a bit more now.

@Lux. I love your screen saver. I like the way it auto resizes to the size of the screen. I do have a question though. I notice at times it goes slightly off the screen without it crashing. Are you making the screen bigger than the screen we can see to stop it crashing?

Asimov

Re: Spirograph what I did from lesson 10

Posted: May 24th, 2012, 4:23 pm
by LuX
Nah. I just modify the PutPixel, so that it only draws pixels withing the screen. This will come later in tutorials, and I don't recommend using it earlier, as the normal PutPixel tells if you messed up something, so it should be used after you are sure you know that you can code stuff correctly.

As for them going outside a bit, it is on purpose, otherwise the larger stars would look stupid when they bounce off when they barely hit the wall.

Fix for the position glitch is btw at the bottom where it says sy = sx[n]; when it should be sy = sy[n];
It should also fix the bug where the stars get stuck at the bottom.

Re: Spirograph what I did from lesson 10

Posted: May 25th, 2012, 1:42 am
by chili
Asimov wrote:I notice that you add the f to 1.000f when using rotations. I believe this is used to show the degree of rotation (I might have typed that number wrong, but the question is still the same heh heh).
LuX wrote:Just one question, like I couldn't google it, but I think its interesting if people write more stuff here:
Is there any difference in 0.001f and 0.001, if the variable is already defined to be a floating number?
Actually, the suffix 'f' here is just a specifier which tells the compiler what data type the literal constant is meant as. If you just type '0.01', the compiler interprets that as a double-precision floating point number (a double). You need to type '0.01f' if you want to specify a single-precision floating point number (a float). A double is 64 bits and and float is 32 bits.
Asimov wrote:I like to know how it works, not that it just works if you know what I mean.
I will be covering this topic in the not-too-distant future (within 6-10 lessons from now I guess). The basic math is this:

x' = x * cos(t) - y * sin(t)
y' = y * cos(t) + x * cos(t)