File: planar.py

package info (click to toggle)
python-visual 1%3A5.12-1.6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 7,708 kB
  • ctags: 7,635
  • sloc: cpp: 15,593; sh: 9,615; ansic: 6,631; python: 4,737; makefile: 384
file content (59 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from visual import *
from random import uniform, randint

scene.forward = (-0.25,-0.25,-1)

nboxes = 8
nlinks = 16
maxgrid = 10

# Create some random cylinders:
nodes = []
for t in arange(-pi,pi,2*pi/nboxes):
  b = cylinder( pos=(10*sin(t),0,10*cos(t)) )
  b.color = b.icolor = (0.5,0.5,1)
  height = uniform(0.5,4)
  b.axis = (0,height,0)
  nodes.append( b )

# Create some random links:
links = []
for l in range(nlinks):
  i = randint(0,nboxes-1)
  j = randint(0,nboxes-1)
  c = curve( ends=[nodes[i].pos,nodes[j].pos], radius=0.2 )
  c.red = 0
  c.green = uniform(0.3,1)
  c.blue = 0
  c.pos = c.ends
  links.append(c)

# Draw a grid:
for i in range(maxgrid+1):
    curve(pos=[(2*i-maxgrid,0,-maxgrid),(2*i-maxgrid,0,maxgrid)], color=color.cyan)       
    curve(pos=[(-maxgrid,0,2*i-maxgrid),(maxgrid,0,2*i-maxgrid)], color=color.cyan)
box(pos=(0,-0.6,0),width=2*maxgrid,length=2*maxgrid,height=1,color=(0,0,0.1))

# Drag and drop loop
drag = None
while 1:
  rate(100)
  if scene.mouse.events:
    c = scene.mouse.getevent()
    if drag and (c.drop or c.click):   # drop the selected object
      newpos = c.project(normal=scene.up, d=yoffset)+offset
      if abs(newpos.x)<=maxgrid and abs(newpos.z)<=maxgrid:
        drag.pos = newpos
      drag.color = drag.icolor
      drag = None
    elif c.pick and hasattr(c.pick,"icolor"):   # pick up the object
      drag = c.pick
      drag.color = color.white
      yoffset = c.pickpos.y
      offset = drag.pos-c.project(normal=scene.up, d=yoffset)
  if drag:
    newpos = scene.mouse.project(normal=scene.up, d=yoffset)+offset
    if abs(newpos.x)<=maxgrid and abs(newpos.z)<=maxgrid:
      drag.pos = newpos
  
  for lnk in links: lnk.pos = lnk.ends