-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchimedean_spiral.nim
99 lines (78 loc) · 2.81 KB
/
Archimedean_spiral.nim
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import math
#import os
import gintro/[glib, gobject, gtk, gio, cairo]
const
Width = 601
Height = 601
Limit = 12 * math.PI
Origin = (x: float(Width div 2), y: float(Height div 2))
B = floor((Width div 2) / Limit)
#-------------------------------------------------------------------------------
proc draw(area: DrawingArea; context: Context) =
## Draw the spiral.
var theta = 0.0
var delta = 0.01
# Clear the region.
context.moveTo(0, 0)
context.setSource(0.0, 0.0, 0.0)
context.paint()
# Draw the spiral.
context.setSource(1.0, 1.0, 0.0)
context.moveTo(Origin.x, Origin.y)
while theta < Limit:
let r = B * theta
let x = Origin.x + r * cos(theta) # X-coordinate on drawing area.
let y = Origin.y + r * sin(theta) # Y-coordinate on drawing area.
context.lineTo(x, y)
context.stroke()
# Set data for next round.
context.moveTo(x, y)
theta += delta
#while gtk.eventsPending(): discard gtk.mainIteration()
#sleep 10
#type argType = tuple[context: Context, ox, oy, B, delta, theta, prevX, prevY: float]
#proc drawLoop(data: pointer): gboolean {.cdecl.} =
# var d = cast[ptr argType](data)
# if d.theta < Limit:
# let r = d.B * d.theta
# let x = d.ox + r * cos(d.theta) # X-coordinate on drawing area.
# let y = d.oy + r * sin(d.theta) # Y-coordinate on drawing area.
# echo d.prevX," ",d.prevY
# echo x," ",y
# d.context.moveTo(d.prevX, d.prevY)
# d.context.lineTo(x, y)
# d.context.stroke()
# d.theta += d.delta
# d.prevX = x
# d.prevY = y
# discard glib.timeout_add(PRIORITY_DEFAULT, 10, drawLoop, data, nil)
# else:
# echo "done"
# d.context.moveTo(10, 10)
# d.context.lineTo(100, 100)
# d.context.stroke()
# dealloc(data)
#var data = create(argType)
#data[] = (context, Origin.x, Origin.y, B, delta, Limit/2, Origin.x, Origin.y)
#discard drawLoop(data)
#-------------------------------------------------------------------------------
proc onDraw(area: DrawingArea; context: Context; data: pointer): bool =
## Callback to draw/redraw the drawing area contents.
area.draw(context)
result = true
#-------------------------------------------------------------------------------
proc activate(app: Application) =
## Activate the application.
let window = app.newApplicationWindow()
window.setSizeRequest(Width, Height)
window.setTitle("Archimedean spiral")
# Create the drawing area.
let area = newDrawingArea()
window.add(area)
# Connect the "draw" event to the callback to draw the spiral.
discard area.connect("draw", ondraw, pointer(nil))
window.showAll()
#-------------------------------------------------------------------------------
let app = newApplication(Application, "Rosetta.spiral")
discard app.connect("activate", activate)
discard app.run()