Sign in to follow this  

converting from 2d space into screen space

This topic is 3727 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

I'm writing up a control for C# .NET, and I'm having a small problem in converting a point in 2d space to a point on the control. Basically, I'm going to be working in a 2d space where there are many objects situated in this space. I'm going to be drawing the objects that are visible onto the control. Visibility is determined by the view point, the position of the center of the control in this 2d space, and how big the viewport is, in 2d space. So, by keeping the control the same size, and increasing/decreasing the view space width and height, we can, in effect, zoom out or in. I'm having problems in figuring out how to convert a point or size into 'screen space', given an object's position, size, and the size of the view space. Has anyone got any hints?

Share this post


Link to post
Share on other sites
If this viewport moves then I know how to do that. just have a point representing the top-left of the view and then when displaying:

realPos = controlPos - viewPortPos

Then render at realPos. As for the zooming effect, you would have to calculate a scaling factor. Say your controls are at 100% of their original size when the viewport is 400px wide and 300px high. You zoom-in to say 100px wide and 75px high. Simply divide one of the original values (either width or height if its fixed view ratio or if its variable (that is the viewport can change proportions eg. from 400, 300 to 400, 400) then both and do it separately) by the new value to get your scale:

Fixed Ratio:

scale = originalWidth / newWidth

Variable Ratio:

xScale = originalWidth / newWidth
yScale = originalHeight / newHeight

Then when rendering your control, multiply its original size by this/these new scale(s):

Fixed Ratio:

controlRenderWidth = controlWidth * scale
controlRenderHeight = controlHeight * scale

Variable Ratio:

controlRenderWidth = controlWidth * xScale
controlRenderHeight = controlHeight * yScale

Does that make sense and is it helpful at all? I feel like it's too easy - as if you could have easily come up with something like this yourself (don't take that the wrong way). Have I missed the point entirely?

Share this post


Link to post
Share on other sites
Well, it does and it doesn't.

What I didn't end up saying was that the units in 2d space are going to be different than in the control, where a unit size would be 1 pixel.

I just thought of an answer and what I came up seems ... well, simple.

Just say you have the View Point, which is the point in 2d space that is centered on the screen, and another point in 2d space. For each component of the coordinate (X,Y), you find out how close the point is to the View Point, in relation to the distance from the View Point to the edge of the screen (horizontal and vertical borders of the control), so you might get something like 0.5 and 0.7.

Then you simply find the point on the control that has that same relative distance to the center of the control.

That should work, and I end up sitting here, wondering why it took me so long to think of it.

But, please, if you have any more ideas, don't stop!

Share this post


Link to post
Share on other sites

This topic is 3727 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this