1

I have the following python/pygame code:

import sys, pygame 
from array import * 
pygame.init() 

height = 600 
width = 800 

Setup Display Window

DISPLAYSURFACE = pygame.display.set_mode((width, height)) 
pygame.display.set_caption('Graphics Assignment Part 1') 

open file

file = open('F:\\school\\Graphics\\PIXA.DAT', 'r') 

read file into a list and close it

vlist = file.readlines() 
#close file 
file.close 
#remove last value from list 
del vlist[-1] 

store values from vlist into points but change J to JUMP bit and split ints with ,

points = [list(map(int,v.split())) if v.strip().lower() != "j" else "JUMP" for v in vlist] 

Setup loops to split vlist into 3 lists of x,y,z coordinate lists

Xs,Ys,Zs= zip(*points) 
unique_Xs = [] 
[unique_Xs.append(val) for val in Xs if val != "J"] 
unique_Ys = [] 
[unique_Ys.append(val) for val in Ys if val != "U"] 
unique_Zs = [] 
[unique_Zs.append(val) for val in Zs if val != "M"] 
draw = list(zip(unique_Xs,unique_Ys)) 

Draw the list of points on the Screen as a series of lines

drawing = pygame.draw.lines(DISPLAYSURFACE,(0,255,172),False, draw, 1) 

Update the Display and quit pygame

pygame.display.update() 
running = True 
while running: 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running = False

I am reading in a list of coordinates and storing them into a list of x,y,z coordinates to draw a set of lines in pygame.

I can draw the image however the whole thing does not show up on the screen because the points are in the range of -2000 to 2000.

How can i change my coordinate range in my python window to display my whole image in my 800x600 display window

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Draw your lines to a `Surface` that's the right size, and then use [`transform.scale`](http://www.pygame.org/docs/ref/transform.html#pygame.transform.scale) (or `smoothscale`) to map it to the display. Alternatively, scale your coordinates before passing them to `draw.lines`. – Gareth Rees Oct 03 '13 at 15:01
  • Formula to convert range to a different range: http://stackoverflow.com/questions/4229662/convert-numbers-within-a-range-to-numbers-within-another-range?lq=1 – ninMonkey Oct 04 '13 at 05:46
  • possible duplicate of [Convert a number range to another range, maintaining ratio](http://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio) – sloth Oct 04 '13 at 08:31

1 Answers1

0

Scale and translate the coordinates. Write a function that maps values for one range to another. See Mapping a range of values to another:

def mapVal(val, amin, amax, bmin, bmax):
    return bmin + (val - amin) * (bmax - bmin) / (amax - amin)
Xs,Ys,Zs= zip(*points) 
   
unique_Xs = [mapVal(val, -2000, 2000, 0, 800) for val in Xs if val != "J"] 
unique_Ys = [mapVal(val, -2000, 2000, 0, 600) for val in Ys if val != "U"]  
unique_Zs = [val for val in Zs if val != "M"]

draw = list(zip(unique_Xs, unique_Ys)) 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174