#### Archived

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

# Simple 2D graphics problem

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

## 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 on other sites
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 on other sites
Hey thanx Josh, that really helps!
Wolf

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633762
• Total Posts
3013726
×