Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168740
Image: ubuntu2004
r = 100 l = -100 t = 1000 b = 100 f = 1000 n = 500 class Ortho: def __init__(self, l, r, t, b, n, f): self.m = matrix(RR, [[2/(r-l), 0, 0, -(r+l)/(r-l)], [0, 2/(t-b), 0, -(t+b)/(t-b)], [0, 0, -2/(f-n), -(f+n)/(f-n)], [0, 0, 0, 1]]) def project(self, vertex): return vertex * self.m class Viewport: def __init__(self, width, height, x=0, y=0): self.x = x self.y = y self.width = width self.height = height def transform(self, pt): xw = (pt[0] + 1)*(self.width / 2.0) + self.x yw = (pt[1] + 1)*(self.height / 2.0) + self.y return (xw, yw) class Canvas: def __init__(self, width, height, x=0, y=0): self.view = Viewport(width, height, x, y) self.vertices= [] def add(self, vx): self.vertices.append(vx) def render2d(self): pts = [(self.view.transform(vx[0]), vx[1]) for vx in self.vertices] visuals = sum([point((vx[0][0], vx[0][1]), rgbcolor=vx[1], pointsize=30, faceted=True) for vx in pts]) visuals.show(frame=True, axes=True, xmin=self.view.x, xmax=self.view.width, ymin=self.view.y, ymax=self.view.height) p = Ortho(l, r, t, b, n, f) canvas = Canvas(640, 480) v = vector ((1, 0, 0, 1)) v2 = vector ((10, 10, 0, 1)) v = p.project(v) v2 = p.project(v2) canvas.add((v, 'red')) canvas.add((v2, 'green')) canvas.render2d()