# [Tutor] Mapping of co-ordinates... pygame

Wayne srilyk at gmail.com
Thu Jul 23 14:58:46 CEST 2009

```On Thu, Jul 23, 2009 at 7:09 AM, Muhammad Ali <ali.jan at gmail.com> wrote:

> Hi,
>
> I have some x and y co-ordinates ranging from (-100, 100) for each x and y.
> Something like this:
>
>                    100
>                      |
>                      |
>                      |
>                      |
> -100------------------------------100
>                      |
>                      |
>                      |
>                      |
>                   -100
>
>  I would like to plot them on a pygame surface.
> I have found that pygame surface is (0, 0) at top right to (width, height)
> at the bottom right corner, like this:
>
> (0,0)                      (width, 0)
> ------------------------------------
> |                                   |
> |                                   |
> |                                   |
> |                                   |
> |                                   |
> |                                   |
> |                                   |
> ------------------------------------
> (0,height)                (width, height)
>
>
> I need a way to map any value from my range to this surface so I could do
> stuff like this:
> pygame.draw.circle(screen, color, (x, y), size
>
> Also my range could vary from -5, 5 or -500 to 500 so I need something
> generic...
>
> It looks like a simple problem but I can't seem to be able to figure it
> out.

Well, first off you'll need to know what your window size is. I presume
you're defining the size and you're defining a 1:1 ratio (400x400, or
600x600 or some such)? If so, then all you need to do is scale your x, y
down and then offset your imaginary grid to the middle. I don't know any of
the pygame methods, but here's what it might look like:

def draw_pixel(x, y):
w = screen.get_width()
h = screen.get_height()
screen.draw_pixel(w/2+x, h/2+y)

That should give you what you need. It starts right in the middle of the
screen (w/2, h/2) then will add your values of x and y. If you have a screen
of 400x400, and x, y of -20, 20 you'll have
200 + (-20) = 180 and 200 + 20 = 220.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090723/292c66c8/attachment.htm>
```