Last active
July 4, 2019 03:14
-
-
Save unclechu/b6c353d5b23114e140ef5577771be0d2 to your computer and use it in GitHub Desktop.
adt_picture.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
Point = tuple[x: float, y: float] | |
PictureKind* = enum | |
Blank, Polygon, Line, Circle, ThickCircle, Arc, | |
ThickArc, Text, Translate, Rotate, Scale, Pictures | |
Picture* = object | |
case kind*: PictureKind | |
of Blank: discard | |
of Polygon: polygonPath*: seq[Point] | |
of Line: linePath*: seq[Point] | |
of Circle: circleRadius*: float | |
of ThickCircle: | |
thickCircleThickness*: float | |
thickCircleRadius*: float | |
of Arc: | |
arcX*: float | |
arcY*: float | |
arcRadius*: float | |
of ThickArc: | |
thickArcX*: float | |
thickArcY*: float | |
thickArcRadius*: float | |
thickArcThickness*: float | |
of Text: text*: string | |
of Translate: | |
translateX*: float | |
translateY*: float | |
translatePicture*: ref Picture | |
of Rotate: | |
rotateDeg*: float | |
rotatePicture*: ref Picture | |
of Scale: | |
scaleX*: float | |
scaleY*: float | |
scalePicture*: ref Picture | |
of Pictures: pictures*: seq[Picture] | |
proc `==`(a: Picture, b: Picture): bool = | |
a.kind == b.kind and ( | |
case a.kind | |
of Blank: true | |
of Polygon: a.polygonPath == b.polygonPath | |
of Line: a.linePath == b.linePath | |
of Circle: a.circleRadius == b.circleRadius | |
of ThickCircle: | |
a.thickCircleThickness == b.thickCircleThickness and | |
a.thickCircleRadius == b.thickCircleRadius | |
of Arc: | |
a.arcX == b.arcX and a.arcY == b.arcY and | |
a.arcRadius == a.arcRadius | |
of ThickArc: | |
a.thickArcX == b.thickArcX and a.thickArcY == b.thickArcY and | |
a.thickArcRadius == b.thickArcRadius and | |
a.thickArcThickness == b.thickArcThickness | |
of Text: a.text == b.text | |
of Translate: | |
a.translateX == b.translateX and a.translateY == b.translateY and | |
a.translatePicture == b.translatePicture | |
of Rotate: | |
a.rotateDeg == b.rotateDeg and a.rotatePicture == b.rotatePicture | |
of Scale: | |
a.scaleX == b.scaleX and a.scaleY == b.scaleY and | |
a.scalePicture == b.scalePicture | |
of Pictures: a.pictures == b.pictures | |
) | |
### assertions ### | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Circle, circleRadius: 5) | |
let b: Picture = Picture(kind: Circle, circleRadius: 5) | |
echo a | |
echo b | |
assert(a == b) | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Circle, circleRadius: 5) | |
let b: Picture = Picture(kind: Circle, circleRadius: 6) | |
echo a | |
echo b | |
assert(a != b) | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Blank) | |
let b: Picture = Picture(kind: Circle, circleRadius: 6) | |
echo a | |
echo b | |
assert(a != b) | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Blank) | |
let b: Picture = Picture(kind: Blank) | |
echo a | |
echo b | |
assert(a == b) | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Pictures, pictures: @[ | |
Picture(kind: Circle, circleRadius: 6), | |
Picture(kind: Circle, circleRadius: 7), | |
]) | |
let b: Picture = Picture(kind: Pictures, pictures: @[ | |
Picture(kind: Circle, circleRadius: 6), | |
Picture(kind: Circle, circleRadius: 7), | |
]) | |
echo a | |
echo b | |
assert(a == b) | |
block: | |
echo "---" | |
let a: Picture = Picture(kind: Pictures, pictures: @[ | |
Picture(kind: Circle, circleRadius: 6), | |
Picture(kind: Circle, circleRadius: 7), | |
]) | |
let b: Picture = Picture(kind: Pictures, pictures: @[ | |
Picture(kind: Circle, circleRadius: 6), | |
Picture(kind: Circle, circleRadius: 8), | |
]) | |
echo a | |
echo b | |
assert(a != b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment