Advertisement

How do I derive in VC++?

Started by March 01, 2000 02:52 AM
5 comments, last by Afgo 24 years, 9 months ago
I''m trying to do a curve-thing and have read what math formulas to use, but I can''t figure out how I use them in Visual C++? What should I type if I want to use F(x) = x^2 for an example? Can anyone give me a code example of how to make something move in a curve?
See my answer in Topic : x^2-curve
Advertisement
yeah, I read that one, but that doesn''t help.. If I for an example have a class that puts a pixel in this way: putpixel(x,y).. what should I write like.. x = ? y = ? I just don''t get how to use the math formulas.
Are you programming with C/C++ or VB or something else?

With C/C++ you need to include the math.h header. After that you use the math formulas declared in that just as if you were using a pocket calculator.

xy : pow(x,y)
x1/2 : sqrt(x)
x2 : x*x (this is faster than using pow(x,y)
sin x : sin(x)
cos x : cos(x)
tan x : tan(x)
sin-1 x : asin(x)
etc...
I think this is what you mean...

You want to graph a function like a graphing calculator does. But in C++. Using that function definition this is how I would do it.

int x = 0; //variable to hold each step
int y = 0; //variable to hold value at each x

//Quick note, this will draw past the screen so you need to make it check to see if the pixel will be drawn out of the screens range before it is drawn (could have messy effects) add this functionality to you putpixel function
for (x = 0; x < screen_width; x++)
{
y = x * x; //x^2
//now watch out, screen coordinates are flipped so you need to adjust
y = -y + (screen_height - 1); //flip the graph along x, then translate down)
putpixel(x,y); //make sure to have that check to see if it is out of the screens bounds
}

That is how it can be done, I am certain there are other ways but try this one.

One other thing, you can change the starting x, or translate it to center in the screen (example)

putpixel(x + (screen_width / 2), y);
..will center it (but will only draw half of the graph using my for definition)
To adjust in this case you would need to define "for" as such

for (x = -(screen_width / 2); x < (screen_width / 2); x++)

I suggest you have a good idea on how transformations work, this will allow you to adjust how the function is graphed so it can be viewed in a better way.

-Omalacon

Edited by - Omalacon on 3/1/00 11:05:10 AM
Things are getting clearer!! =) But not clear enough for me I guess...! what if I, let''s say have a starting position at the coordinates (200,200) pixels and want the curve to be a certain length and height... (I''m trying to do a fountain-effect thing)
Advertisement
It''s not the programming you''re having problem with. It''s the math.

I would like to suggest you do it as I wrote in Topic: x^2-curve. You set the start position to (200,200) and the start speed to (rnd, rnd). Where rnd is a random number generated for each particle. Then for each frame you add the speed to the position and the decrease the speed''s y-component with a small number. This will give you the effect of gravity pulling on the particles.

To make it more like a fountain you must make sure that the initial speed is constrained to up turned cone. This is easily done by adding a constant to the y-component of the initial speed.

This topic is closed to new replies.

Advertisement