Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Tranberg
14
Years of Service
User Offline
Joined: 27th Feb 2012
Location:
Posted: 27th Feb 2012 23:48
How do i get darkbasic to draw a given equation like:

4x^2 + 2x^3 + 12 = 0
SoftMotion3D
AGK Developer
20
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 28th Feb 2012 03:00 Edited at: 28th Feb 2012 03:02
How do we get a quad post.... humm noobie?

Try to be patient and try not to even double post...lol
Im sure someone can help


edit:ok it was 3...not 4

Can you be more specific on what your trying to do?

2d graphics? 3d? any code to share?

1beginner1
17
Years of Service
User Offline
Joined: 28th Jun 2009
Location: Belgium
Posted: 28th Feb 2012 10:21 Edited at: 28th Feb 2012 21:09
Maybe I can help, if you mean something like this give in the coefficients (4,0,-3.5,2) and that program create a polynomial like (4 -3.5x^2 + 2x^3). But the file I'm having is written in java maybe that I can convert it, needed it for a schoolproject. It can also evalaute the specific polynomial with a given value and derivate the function.
It can take a few days before I post a message about succeeding in it or failing in converting it in darkbasic pro.

the java file looks like this, stripped down the file with unnecessary function, atleast for me.


EDIT: Ok here is the code for darkbasic pro, atleast if you need this. Ran only into some problems, double float and float are a bit unaccurate. And I cannot send a array to a function so I needed to declare it global. But that just destroys the needs of having a seperate function for it so you just can put inside your main code. You can easily make the array larger if you want and read the values in when you run the program instead of doing it inside the code.

29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 28th Feb 2012 22:08
I took the original post to mean drawing a graph of the equation. If this is the case then what you have to do is take what ever the equation is and then calculate values of y based on chosen values of x, this is what 1beginner1 code is essentially doing but for a single value of x.

So for your eqation: y = 4x^2 + 2x^3 + 12 you would step through values of x and calculate values for y.

There are different ways of drawing the graph, the method I use is to calculate points along the graph and then join the points with straight lines. If you increment the values of x in suitably small amounts the curve will appear smooth as the distance between points will be short. You could use dots but I find this more of a hassle as you end up having to fill in the gaps between the dots.

An example of how to do all this:




I've used a sine wave as it's a little more fun than a quadratic but you can swap the equation for anything you want.

There are a couple of things to look out for. First, in 2d the origin is at the top left of the screen and postive y is downward. This has an effect of flipping the graph over (like a reflection of a mountain viewed on the surface of a flat lake on a cold crisp morning with nothing to do but... sorry, getting side tracked ). The second thing to look out for is the scale of the x axis, if you swap the sine equation for your quadratic, then what happens is you get a very steep curve that "hugs" the left hand side of the screen. To stretch the curve out a bit you will need to scale the x coordinate by a suitable multiplication factor but this multiplication factor can not interfere with the equation when calculating values of y, it has a purely visual role. The third is the position the zero coordinate if you want to show draw the x and y axis to the screen. Essentially the "+100" in the equation I used moves the entire graph down the screen by 100 pixels. So if you wanted to have an equation "y# = sin(x#)" then half the graph will be off the top of the screen (off the top of the screen are negative values of y). Also, if you want the x axis to start, say, ten pixels from the left edge of the screen then you would have to add this offset to the x coordinate for drawing purposes. Again this offset can not be allowed to interfere with the equation itself but is purely to sort out the visuals. In other words, what you might have to do is split the values you are calculating from the values that you use to plot the graph to the screen.

Ok, that's a big chuck of writing that probably doesn't make sense. So I suggest that you play with the code, try out some different equations and see what you can make of it. If this is what you were asking for in the first place.

Login to post a reply

Server time is: 2026-07-10 00:43:43
Your offset time is: 2026-07-10 00:43:43