Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wolfbane

Simple 2D graphics problem

This topic is 6076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok myquestion is rather simple since I am new to graphics programming. I am trying to write a simple program that would take common math equasions such as a parabolla y=x^2 and display the resulting curve on the screen. The problem is that the screen coords orientate at the top left and are incremented in whole numbers. So how do i get around this? I am using Java and was wondering if there was a special class library for this? or mayb there is a trick or some algorithm that would solve the prob. Does anyone know? If so please help. Thanx.

Share this post


Link to post
Share on other sites
Advertisement
Okay, so what you have are your X SCREEN coordinates

1 2 3 4 5 6 7 8 9 10 11 ... 640

And you want to convert them to coordinates in your math view of say, -10 to 10. So You would say

So you have:
screenX = The x coordinate (Screen coordinates) that you want to graph for. (Use floating point arithmetic for this, of course)

screenMinX = the min. X coordinate in drawable range
screenMaxX - the max. X coordinate in drawable range
graphMinX = the min. X coordinate displayed by the graph
graphMaxX = the max. X coordinate displayed by the graph
graphX = The x coordinate you''re getting. Coordinate on the graph

graphX = (screenX-screenMinX) / (screenMaxX-screenMinX) * (graphMaxX-graphMinX) + graphMinX;

THen you plug graphX into your equation (whatever it may be) and get a graph Y equation, and do basically the opposite conversion

screenY = (graphY-graphMinY) / (graphMaxY-graphMinY) * (screenMaxX-screenMinX) + screenMinX;

to get the on-screen Y coordinate. Then simply plot that value.

Hope this helps!

Josh

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!