from math import * # translation for the trackball T = [0,0,0] # use this to initialize camera position and to implement zoom # rotation for the trackball R = [[1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]] # old mouse x,y coordinates oxy = [0,0] def project(r, x, y): """Project an x,y pair onto a sphere of radius r if we are within sqrt(2) * r from center OR a hyperbolic sheet if we are farther away.""" dd = x*x+y*y rr = r*r if dd < .5 * rr: # on sphere return sqrt(rr - dd) else: # on hyperbola return .5 * rr / sqrt(dd) def rotation(w, h, x, y, ox, oy): """Given screen size, new and old mouse coordinates, calculate the new rotation for the trackball.""" # center of the screen c = [w/2, h/2] # radius is the half the smaller screen extent if w < h: r = c[0] else: r = c[1] # mouse point in screen centered coords x,y = x - c[0], h-1-y - c[1] ox,oy = ox - c[0], h-1-oy - c[1] # get 3d points by projecting from screen p1 = [ox, oy, project(r, ox, oy)] p2 = [x, y, project(r, x, y)] # the origin is at the center of the sphere # calculate the angle between p1 and p2 # your code here # calculate the rotation axis for rotating p1 to p2 # hint: the axis is perpendicular to both p1 and p2 # your code here, compound the new rotation with the # old one, stored in R def zoom(y, oy): """Given old and new y coordinate, modify the translation of the trackball for zoom effect.""" # a good way to implement the zooming is the modify the # camera's distance from the origin by a factor of # e.g., 2^((y - oy) / 80.0) pass def pan(x,y,ox,oy): # how far in the viewplane are pixels apart? scale panning accordingly pass