forked from fossunited/joy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joy.py
1019 lines (738 loc) · 25.9 KB
/
joy.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Joy
===
Joy is a tiny creative coding library in Python.
BASIC USAGE
An example of using joy:
>>> from joy import *
>>>
>>> c = circle(x=100, y=100, r=50)
>>> show(c)
The `circle` function creates a new circle and the `show` function
displays it.
PRINCIPLES
Joy follows functional programming approach for its interface. Each
function/class gives a shape and those shapes can be transformed and
combined using other utility functions.
By design, there is no global state in the library.
Joy uses SVG to render the shapes and the shapes are really a very thin
wrapper over SVG nodes. It is possible to use every functionality of SVG,
even if that is not exposed in the API.
COORDINATE SYSTEM
Joy uses a canvas with (0, 0) as the center of the canvas.
By default the size of the canvas is (300, 300).
BASIC SHAPES
Joy supports `circle`, `rectangle` and `line` as basic shapes.
>>> c = circle(x=100, y=100, r=50)
>>> r = rectangle(x=0, y=0, w=200, h=200)
>>> show(c, r)
All basic shapes have default values of all the arguments, making it
easier to start using them.
>>> c = circle()
>>> r = rectangle()
>>> z = line()
>>> show(c, r, z)
COMBINING SHAPES
The `+` operator is used to combine multiple shapes into a
single shape.
>>> shape = circle() + rectangle()
>>> show(shape)
TRANSFORMATIONS
Joy supports `translate`, `rotate` and `scale` transformations.
The `translate` transformation moves the given shape by `x` and `y`.
>>> c1 = circle(r=50)
>>> c2 = c1 | translate(x=100, y=0)
>>> show(c1, c2)
As you've seen the above example, transformations are applied using
the `|` operator.
The `Rotate` transformation rotates a shape anti-clockwise by the specified
angle.
>>> shape = rectangle() | rotate(angle=45)
>>> show(shape)
The `Scale` transformation scales a shape.
>>> shape = circle() | scale(x=1, y=0.5)
>>> show(shape)
HIGER ORDER TRANSFORMATIONS
Joy supports a transform called `repeat` to apply a transformation multiple times
and combining all the resulting shapes.
>>> flower = rectangle() | repeat(18, rotate(10))
>>> show(flower)
JUPYTER LAB INTEGRATION
Joy integrates very well with Jupyter notebooks and every shape is
represented as SVG image by jupyter.
"""
import html
import itertools
import random as random_module
import string
__version__ = "0.4.0"
__author__ = "Anand Chitipothu <anandology@gmail.com>"
SQRT2 = 2**0.5
# Random suffix to avoid conflicts between ids of multiple sketches in the same page
ID_SUFFIX = "".join(random_module.choice(string.ascii_letters+string.digits) for i in range(4))
_shape_counter = itertools.count()
class Shape:
"""Shape is the base class for all shapes in Joy.
A Shape is an SVG node and supports converting itself into svg text.
Typically, users do not interact with this class directly, but use it
through its subclasses.
"""
def __init__(self, tag, children=None, transform=None, **attrs):
"""Creates a new shape.
"""
self.tag = tag
self.children = children
self.attrs = attrs
self.transform = None
def _get_next_shape_id(self):
id = next(_shape_counter)
return f"s-{id}-{ID_SUFFIX}"
def get_reference(self):
if not "id" in self.attrs:
self.attrs["id"] = self._get_next_shape_id()
attrs = {"xlink:href": "#" + self.id}
return Shape("use", **attrs)
def __repr__(self):
return f"<{self.tag} {self.attrs}>"
def __getattr__(self, name):
if not name.startswith("_") and name in self.attrs:
return self.attrs[name]
else:
raise AttributeError(name)
def apply_transform(self, transform):
if self.transform is not None:
transform = self.transform | transform
shape = self.clone()
shape.transform = transform
return shape
def clone(self):
shape = object.__new__(self.__class__)
shape.__dict__.update(self.__dict__)
# don't share attrs on clone
# also remove the id as the new nodes gets a new id
shape.attrs = dict(self.attrs)
shape.attrs.pop("id", None)
return shape
def get_attrs(self):
attrs = dict(self.attrs)
if self.transform:
attrs['transform'] = self.transform.as_str()
return attrs
def as_dict(self):
d = self.get_attrs()
d['tag'] = self.tag
if self.children:
d['children'] = [n.as_dict() for n in self.children]
return d
def _svg(self, indent="") -> str:
"""Returns the svg representation of this node.
This method is used to recursively construct the svg of a node
and it's children.
>>> c = Shape(tag='circle', cx=100, cy=100, r=50)
>>> c._svg()
'<circle cx="100" cy="100" r="50" />'
"""
attrs = self.get_attrs()
if self.children:
tag_text = render_tag(self.tag, **attrs, close=False)
return (
indent + tag_text + "\n" +
"".join(c._svg(indent + " ") for c in self.children) +
indent + "</" + self.tag + ">\n"
)
else:
tag_text = render_tag(self.tag, **attrs, close=True)
return indent + tag_text + "\n"
def as_svg(self, width=300, height=300) -> str:
"""Renders this node as svg image.
The svg image is assumed to be of size (300, 300) unless the
width and the height arguments are provided.
Example:
>>> c = Shape(tag='circle', cx=100, cy=100, r=50)
>>> print(c.as_svg())
<svg width="300" height="300" viewBox="-150 -150 300 350" fill="none" stroke="black" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="50" />
</svg>
"""
return SVG([self], width=width, height=height).render()
def __add__(self, shape):
if not isinstance(shape, Shape):
return NotImplemented
return Group([self, shape])
def _repr_svg_(self):
"""Returns the svg representation of this node.
This method is called by Juputer to render this object as an
svg image.
"""
return self.as_svg()
class SVG:
"""SVG renders any svg element into an svg image.
"""
def __init__(self, nodes, width=300, height=300):
self.nodes = nodes
self.width = width
self.height = height
def render(self):
attrs = {
"tag": "svg",
"width": self.width,
"height": self.height,
"viewBox": f"-{self.width//2} -{self.height//2} {self.width} {self.height}",
"fill": "none",
"stroke": "black",
"xmlns": "http://www.w3.org/2000/svg",
"xmlns:xlink": "http://www.w3.org/1999/xlink"
}
svg_header = render_tag(**attrs)+ "\n"
svg_footer = "</svg>\n"
# flip the y axis so that y grows upwards
node = Group(self.nodes) | Scale(sx=1, sy=-1)
return svg_header + node._svg() + svg_footer
def _repr_svg_(self):
return self.render()
def __str__(self):
return self.render()
def __repr__(self):
return "SVG:{self.nodes}"
class Point:
"""Creates a new Point.
Point represents a point in the coordinate space and it contains
attributes x and y.
>>> p = Point(x=100, y=50)
"""
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, p):
return isinstance(p, Point) \
and p.x == self.x \
and p.y == self.y
def __repr__(self):
return f"Point({self.x}, {self.y})"
class Circle(Shape):
"""Creates a circle shape.
Parameters:
center:
The center point of the circle.
Defaults to Point(0, 0) when not specified.
radius:
The radius of the circle.
Defaults to 100 when not specified.
Examples:
Draw a circle.
>>> c = Circle()
>>> show(c)
Draw a Circle with radius 50.
>>> c = Circle(radius=50)
>>> show(c)
Draw a circle with center at (100, 100) and radius as 50.
>>> c = Circle(center=Point(x=100, y=100), radius=50)
>>> show(c)
When no arguments are specified, it uses (0, 0) as the center and
100 as the radius.
"""
def __init__(self, center=Point(0, 0), radius=100, **kwargs):
self.center = center
self.radius = radius
cx, cy = self.center.x, self.center.y
super().__init__("circle",
cx=cx,
cy=cy,
r=self.radius,
**kwargs)
class Ellipse(Shape):
"""Creates an ellipse shape.
Parameters:
center:
The center point of the ellipse. Defaults to (0, 0) when
not specified.
width:
The width of the ellipse. Defaults to 100 when not
specified.
height:
The height of the ellipse. Defaults to 100 when not
specified.
Examples:
Draw an ellipse with center at origin and width of 200 and height of 100:
>>> r = Ellipse()
>>> show(r)
Draw an ellipse having a width of 100 and a height of 50.
>>> r = Ellipse(width=100, height=50)
>>> show(r)
Draw an ellipse centered at (100, 100) and with a width
of 200 and height of 100.
>>> r = Ellipse(center=Point(x=100, y=100), width=200, height=100)
>>> show(r)
"""
def __init__(self, center=Point(0, 0), width=200, height=100, **kwargs):
self.center = center
self.width = width
self.height = height
cx, cy = self.center.x, self.center.y
rx = width/2
ry = height/2
super().__init__(
tag="ellipse",
cx=cx,
cy=cy,
rx=rx,
ry=ry,
**kwargs)
class Rectangle(Shape):
"""Creates a rectangle shape.
Parameters:
center:
The center point of the rectangle. Defaults to (0, 0) when
not specified.
width:
The width of the rectangle. Defaults to 200 when not
specified.
height:
The height of the rectangle. Defaults to 100 when not
specified.
Examples:
Draw a rectangle:
>>> r = Rectangle()
>>> show(r)
Draw a square.
>>> r = Rectangle(width=200, height=200)
>>> show(r)
Draw a rectangle centered at (100, 100) and with a width
of 200 and height of 100.
>>> r = Rectangle(center=Point(x=100, y=100), width=200, height=100)
>>> show(r)
"""
def __init__(self, center=Point(0, 0), width=200, height=100, **kwargs):
self.center = center
self.width = width
self.height = height
cx, cy = self.center.x, self.center.y
x = cx - width/2
y = cy - height/2
super().__init__(
tag="rect",
x=x,
y=y,
width=width,
height=height,
**kwargs)
class Line(Shape):
"""Basic shape for drawing a line connecting two points.
Parameters:
start:
The starting point of the line. Defaults to (-100, 0) when
not specified.
end:
The ending point of the line. Defaults to (100, 0) when not
specified.
Examples:
Draw a line:
>>> z = line()
>>> show(z)
Draw a line from (0, 0) to (100, 50).
>>> z = line(start=Point(x=0, y=0), end=Point(x=100, y=50))
>>> show(z)
"""
def __init__(self, start=Point(-100, 0), end=Point(100, 0), **kwargs):
self.start = start
self.end = end
x1, y1 = self.start.x, self.start.y
x2, y2 = self.end.x, self.end.y
super().__init__("line", x1=x1, y1=y1, x2=x2, y2=y2, **kwargs)
class Group(Shape):
"""Creates a container to group a list of shapes.
This class is not meant for direct consumption of the users. Users
are recommended to use `combine` to combine multiple shapes and use
`translate`, `rotate` and `scale` for doing transformations.
This creates an svg <g> element.
Parameters:
shapes:
The list of shapes to group.
Examples:
Combine a circle and a rectangle.
>> c = Circle()
>> r = Rectangle()
>>> shape = Group([c, r])
>>> show(shape)
Shapes can also be combined using the + operator and that creates
a group implicitly.
>>> shape = Circle() + Rectangle()
>>> show(shape)
"""
def __init__(self, shapes, **kwargs):
super().__init__("g", children=shapes, **kwargs)
def render_tag(tag, *, close=False, **attrs):
"""Renders a html/svg tag.
>>> render_tag("circle", cx=0, cy=0, r=10)
'<circle cx="0" cy="0" r="10">'
When `close=True`, the tag is closed with "/>".
>>> render_tag("circle", cx=0, cy=0, r=10, close=True)
'<circle cx="0" cy="0" r="10" />'
Underscore characters in the attribute name are replaced with hypens.
>>> render_tag("circle", cx=0, cy=0, r=10, stroke_width=2)
'<circle cx="0" cy="0" r="10" stroke-width="2">'
"""
end = " />" if close else ">"
if attrs:
items = [(k.replace("_", "-"), html.escape(str(v))) for k, v in attrs.items() if v is not None]
attrs_text = " ".join(f'{k}="{v}"' for k, v in items)
return f"<{tag} {attrs_text}{end}"
else:
return f"<{tag}{end}"
class Transformation:
def apply(self, shape):
return shape.apply_transform(self)
def join(self, transformation):
return TransformationList([self, transformation])
def __or__(self, right):
if not isinstance(right, Transformation):
return NotImplemented
return self.join(transformation=right)
def __ror__(self, left):
if not isinstance(left, Shape):
return NotImplemented
return self.apply(shape=left)
class TransformationList(Transformation):
def __init__(self, transformations):
self.transformations = transformations
def join(self, transformation):
return TransformationList(self.transformations + [transformation])
def as_str(self):
# Reversing the transformations as SVG applies them in the
# reverse order (the rightmost is appled first)
return " ".join(t.as_str() for t in self.transformations[::-1])
class Translate(Transformation):
"""Creates a new Translate transformation that moves a shape by
x and y when applied.
Parameters:
x:
The number of units to move in the x direction
y:
The number of units to move in the y direction
Example:
Translate a circle by (100, 50).
>>> c = Circle() | Translate(100, 50)
>>> show(c)
"""
def __init__(self, x, y):
self.x = x
self.y = y
def as_str(self):
return f"translate({self.x} {self.y})"
class Rotate(Transformation):
"""Creates a new rotate transformation.
When applied to a shape, it rotates the given shape by angle, around
the anchor point.
Parameters:
angle:
The angle to rotate the shape in degrees.
anchor:
The anchor point around which the rotation is performed.
Examples:
Rotates a square by 45 degrees.
>>> shape = Rectangle() | Rotate(angle=45)
>>> show(shape)
Rotate a rectangle around its top-left corner and
combine with itself.
>>> r1 = Rectangle()
>>> r2 = r1 | Rotate(angle=45, anchor=(r.point[0]))
>>> shape = combine(r1, r2)
>>> show(shape)
"""
def __init__(self, angle, anchor=Point(0, 0)):
self.angle = angle
self.anchor = anchor
def as_str(self):
origin = Point(0, 0)
if self.anchor == origin:
return f"rotate({self.angle})"
else:
return f"rotate({self.angle} {self.anchor.x} {self.anchor.y})"
class Scale(Transformation):
"""Creates a new scale transformation.
Parameters:
sx:
The scale factor in the x direction.
sy:
The scale factor in the y direction. Defaults to
the value of sx if not provided.
"""
def __init__(self, sx, sy=None):
if sy is None:
sy = sx
self.sx = sx
self.sy = sy
def as_str(self):
return f"scale({self.sx} {self.sy})"
class Repeat(Transformation):
"""Repeat is a higher-order transformation that repeats a
transformation multiple times.
Parameters:
n:
The number of times to rotate. This also determines the
angle of each rotation, which will be 360/n.
transformation:
The transformation to apply repeatedly.
Examples:
Draw three circles:
>>> shape = Circle(radius=25) | Repeat(4, Translate(x=50, y=0))
>>> show(shape)
Rotate a line multiple times:
>>> shape = Line() | Repeat(36, Rotate(angle=10))
>>> show(shape)
Rotate and shrink a line multiple times:
>>> shape = Line() | Repeat(18, Rotate(angle=10) | Scale(sx=0.9))
>>> show(shape)
"""
def __init__(self, n, transformation):
self.n = n
self.transformation = transformation
def apply(self, shape):
ref = shape.get_reference()
defs = Shape("defs", children=[shape])
return defs + self._apply(ref, self.transformation, self.n)
def _apply(self, shape, tf, n):
if n == 1:
return shape
else:
result = self._apply(shape, tf, n-1) | tf
return shape + result
class Cycle(Transformation):
"""
Rotates the given shape repeatedly and combines all the resulting
shapes.
The cycle function is very amazing transformation and it creates
surprising patterns.
Parameters:
n:
The number of times to rotate. This also determines the
angle of each rotation, which will be 360/n.
anchor:
The anchor point for the rotation. Defaults to (0, 0) when
not specified.
s:
Optional scale factor to scale the shape for each rotation.
This can be used to grow or shrink the shape while rotating.
angle:
Optional angle of rotation. Defaults to 360/n when not
specified,
Examples:
Cycle a line:
>>> shape = Line() | Cycle()
>>> show(shape)
Cycle a square:
>>> shape = Rectangle() | Cycle()
>>> show(shape)
Cycle a rectangle:
>>> shape = Rectangle(width=200, height=100) | Cycle()
>>> show(shape)
Cycle an ellipse:
>>> e = scale(Circle(), sx=1, sy=0.5)
>>> show(e | Cycle())
Create a spiral with shrinking squares:
>>> shape = Rectangle(width=300, height=300) | cycle(n=72, s=0.92)
>>> show(shape)
"""
def __init__(self, n=18, anchor=Point(x=0, y=0), s=None, angle=None):
self.n = n
self.angle = angle if angle is not None else 360/n
self.anchor = anchor
self.s = s
def apply(self, shape):
shapes = [shape | Rotate(angle=i*self.angle, anchor=self.anchor) for i in range(self.n)]
if self.s is not None:
shapes = [shape_ | Scale(sx=self.s**i) for i, shape_ in enumerate(shapes)]
return Group(shapes)
def show(*shapes):
"""Shows the given shapes.
It also adds a border to the canvas and axis at the origin with
a light color as a reference.
Parameters:
shapes:
The shapes to show.
Examples:
Show a circle:
>>> show(circle())
Show a circle and square.
>>> c = circle()
>>> s = rect()
>>> show(c, s)
"""
markers = [
Rectangle(width=300, height=300, stroke="#ddd"),
Line(start=Point(x=-150, y=0), end=Point(x=150, y=0), stroke="#ddd"),
Line(start=Point(x=0, y=-150), end=Point(x=0, y=150), stroke="#ddd")
]
shapes = markers + list(shapes)
img = SVG(shapes)
from IPython.display import display
display(img)
def circle(x=0, y=0, r=100, **kwargs):
"""Creates a circle with center at (x, y) and radius of r.
Examples:
Draw a circle.
c = circle()
show(c)
Draw a circle with radius 50.
c = circle(r=50)
show(c)
Draw a circle with center at (10, 20) and a radius of 50.
c = circle(x=10, y=20, r=50)
show(c)
"""
return Circle(center=Point(x=x, y=y), radius=r, **kwargs)
def rectangle(x=0, y=0, w=200, h=100, **kwargs):
"""Creates a rectangle with center at (x, y), a width of w and a height of h.
Examples:
Draw a rectangle.
r = rectangle()
show(r)
Draw a rectangle with width of 100 and height of 50.
r = rectangle(w=100, h=50)
show(r)
Draw a rectangle with center at (10, 20), a width of 100 and a height of 50.
r = rectangle(x=10, y=20, w=100, h=50)
show(r)
"""
return Rectangle(center=Point(x=x, y=y), width=w, height=h, **kwargs)
def ellipse(x=0, y=0, w=200, h=100, **kwargs):
"""Creates an ellipse with center at (x, y), a width of w and a height of h.
Examples:
Draw an ellipse.
r = ellipse()
show(r)
Draw an ellipse with a width of 100 and height of 50.
r = ellipse(w=100, h=50)
show(r)
Draw an ellipse with center at (10, 20), a width of 100 and a height of 50.
r = ellipse(x=10, y=20, w=100, h=50)
show(r)
"""
return Ellipse(center=Point(x=x, y=y), width=w, height=h, **kwargs)
def line(x1=None, y1=None, x2=None, y2=None, **kwargs):
"""Creates a line from point (x1, y1) to point (x2, y2).
Examples:
Draw a line.
z = line()
Draw a line from (10, 20) to (100, 200)
z = line(x1=10, y1=20, x2=100, y2=200)
"""
if x1 is None and y1 is None and x2 is None and y2 is None:
x1, y1 = -100, 0
x2, y2 = 100, 0
else:
pairs = dict(x1=x1, y1=y1, x2=x2, y2=y2)
missing = [name for name, value in pairs.items() if value is None]
if missing:
raise Exception("missing arguments for line: ", ", ".join(missing))
return Line(start=Point(x1, y1), end=Point(x2, y2), **kwargs)
def point(x, y):
"""Creates a Point with x and y coordinates.
"""
return Point(x, y)
def polygon(points, **kwargs):
"""Creates a polygon with given list points.
Example:
p1 = point(x=0, y=0)
p2 = point(x=100, y=0)
p3 = point(x=0, y=100)
triangle = polygon([p1, p2, p3])
show(triangle)
"""
points_str = " ".join(f"{p.x},{p.y}" for p in points)
return Shape(tag="polygon", points=points_str, **kwargs)
def polyline(points, **kwargs):
"""Creates a polyline with given list points.
Example:
p1 = point(x=-50, y=50)
p2 = point(x=0, y=-25)
p3 = point(x=0, y=25)
p4 = point(x=50, y=-50)
line = polyline([p1, p2, p3, p4])
show(line)
"""
points_str = " ".join(f"{p.x},{p.y}" for p in points)
return Shape(tag="polyline", points=points_str, **kwargs)
def translate(x=0, y=0):
"""Translates a shape.
Examples:
Translate a shape by 10 units in x direction.
shape = circle() | translate(x=10)
Translate a shape by 10 units in y direction.
shape = circle() | translate(y=10)
Translate a shape by 10 units in x direction and 20 units in y direction.
shape = circle() | translate(x=10, y=20)
"""
return Translate(x=x, y=y)
def scale(s=None, x=1, y=1):
"""Scales a shape.
Examples:
Scale a shape in both x and y directions:
shape = circle() | scale(0.5)
Scale a shape in only in x direction:
shape = circle() | scale(x=0.5)
Scale a shape in only in y direction:
shape = circle() | scale(y=0.5)
Scale a shape differently in x and y directions:
shape = circle() | scale(x=0.5, y=0.75)
"""
if s is not None:
return Scale(sx=s, sy=s)
else:
return Scale(sx=x, sy=y)
def rotate(angle):
"""Rotates a shape.
Examples:
Rotate a shape by 30 degrees
shape = line() | rotate(30)
"""
return Rotate(angle)
def repeat(n, transformation):
"""Repeats a transformation multiple times on a shape.
Examples:
Repeatly rotate a line 9 times by 10 degrees.
shape = line() | repeat(9, rotate(10))
"""
return Repeat(n, transformation)
def combine(shapes):
"""The combine function combines a list of shapes into a single shape.
Example:
>>> shapes = [circle(r=50), circle(r=100), circle(r=150)]
>>> shape = combine(shapes)
>>> show(shape)
"""
return Group(shapes)
def color(r, g, b, a=None):
"""Creates a color with given r g b values.
Parameters:
r - the red component of the color, allowed range is 0-255.
g - the green component of the color, allowed range is 0-255.
b - the blue component of the color, allowed range is 0-255.
a - optional argument to indicate the transparency or the
alpha value. The allowed range is 0-1.
"""
if a is None:
return f"rgb({r}, {g}, {b})"
else:
return f"rgba({r}, {g}, {b}, {a})"
def random(a=None, b=None):
"""Creates a random number.
The random function can be used in three ways:
random() # returns a random number between 0 and 1