├── .github └── FUNDING.yml ├── .gitignore ├── .goreleaser.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── _examples ├── Makefile ├── arc.g2d ├── arc.png ├── arrows.g2d ├── arrows.png ├── circles.g2d ├── circles.png ├── ellipse.g2d ├── ellipse.png ├── grid.g2d ├── grid.png ├── heart.g2d ├── heart.png ├── lines.g2d ├── lines.png ├── logo.g2d ├── logo.png ├── maurer.g2d ├── maurer.png ├── none.g2d ├── rect.g2d ├── rect.png ├── sierpinsky.g2d ├── sierpinsky.png ├── snowflake.g2d ├── snowflake.png ├── spectre.g2d ├── spectre.png ├── spiral.g2d ├── spiral.png ├── star.g2d ├── star.png ├── targets.g2d └── targets.png ├── _extras └── gtksourceview-3.0 │ ├── language-specs │ └── g2d.lang │ └── styles │ └── vsdark.xml ├── ast ├── ast.go └── ast_test.go ├── builtins ├── builtins.go ├── calc │ ├── abs.go │ ├── abs_test.go │ ├── atan.go │ ├── atan2.go │ ├── atan2_test.go │ ├── atan_test.go │ ├── cos.go │ ├── degrees.go │ ├── hypot.go │ ├── lerp.go │ ├── lerp_test.go │ ├── map.go │ ├── max.go │ ├── max_test.go │ ├── min.go │ ├── min_test.go │ ├── pow.go │ ├── radians.go │ ├── rand.go │ ├── sin.go │ └── sqrt.go ├── core │ ├── array.go │ ├── exit.go │ ├── input.go │ ├── len.go │ ├── print.go │ └── types.go └── graphics │ ├── context.go │ ├── helpers.go │ ├── image.go │ ├── path.go │ ├── primitives.go │ ├── text.go │ └── transform.go ├── cmd ├── eval.go ├── eval_test.go └── root.go ├── data ├── data.go └── data_test.go ├── eval ├── env_test.go ├── eval.go └── eval_test.go ├── gg ├── context.go ├── img │ ├── bezier.go │ ├── context.go │ ├── path.go │ ├── pattern.go │ ├── point.go │ └── util.go ├── matrix.go ├── pattern.go └── wrap.go ├── go.mod ├── go.sum ├── lexer ├── lexer.go └── lexer_test.go ├── logo.png ├── main.go ├── object ├── array.go ├── bool.go ├── builtin.go ├── environment.go ├── error.go ├── float.go ├── function.go ├── image.go ├── int.go ├── null.go ├── object.go └── str.go ├── parser ├── parser.go ├── parser_test.go └── parser_tracing.go ├── testdata └── none.g2d ├── token ├── token.go └── token_test.go ├── typing └── typing.go └── utils └── utils.go /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # repo: lucasepe/g2d 2 | # filename: FUNDING.YML 3 | 4 | github: lucasepe -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Intellij 2 | .idea/**/workspace.xml 3 | .idea/**/tasks.xml 4 | .idea/**/encodings.xml 5 | .idea/**/compiler.xml 6 | .idea/**/misc.xml 7 | .idea/**/modules.xml 8 | .idea/**/vcs.xml 9 | 10 | ## VSCode 11 | .vscode/ 12 | 13 | ## File-based project format: 14 | *.iws 15 | *.iml 16 | .idea/ 17 | 18 | # Binaries for programs and plugins 19 | *.exe 20 | *.exe~ 21 | *.dll 22 | *.so 23 | *.dylib 24 | *.dat 25 | *.DS_Store 26 | 27 | # Test binary, built with `go test -c` 28 | *.test 29 | 30 | # Output of the go coverage tool, specifically when used with LiteIDE 31 | *.out 32 | 33 | # Goreleaser builds 34 | **/dist/** 35 | 36 | # This is my wip ideas folder 37 | experiments/** 38 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | # This is an example goreleaser.yaml file with some sane defaults. 2 | # Make sure to check the documentation at http://goreleaser.com 3 | # Run locally with: goreleaser --rm-dist --snapshot --skip-publish 4 | project_name: g2d 5 | 6 | before: 7 | hooks: 8 | - go mod tidy 9 | - go mod download 10 | 11 | builds: 12 | - binary: '{{ .ProjectName }}' 13 | main: ./main.go 14 | env: 15 | - CGO_ENABLED=0 16 | ldflags: 17 | - -s -w -X main.Version={{.Version}} -X main.GitCommit={{.Commit}} 18 | - -a -extldflags "-static" 19 | goos: 20 | - windows 21 | - linux 22 | - darwin 23 | goarch: 24 | - amd64 25 | 26 | archives: 27 | - replacements: 28 | darwin: macOS 29 | files: 30 | - LICENSE 31 | 32 | nfpms: 33 | - 34 | package_name: g2d 35 | vendor: Luca Sepe 36 | homepage: https://lucasepe.it/ 37 | maintainer: Luca Sepe 38 | description: Geometry art built coding. 39 | license: MIT 40 | replacements: 41 | amd64: 64-bit 42 | formats: 43 | - deb 44 | - rpm 45 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 0.5.0 (Dec 28, 2020) 2 | 3 | NEW FEATURES: 4 | 5 | - commandline tool can load `.g2d` scripts from your local filesystem or from HTTP (see [README](README.md)) 6 | - `viewport(...)` builtin function takes two more optional parameters `xOffset` and `yOffset` to eventually relocate the viewport 7 | - new builtin function: `fillColor` to specify the fill color (takes `r`, `g`, `b`, `[a]` range [`0-255`] or hex color string `#rrggbbaa`) 8 | - new builtin function: `strokeColor` to specify the stroke color (takes `r`, `g`, `b`, `[a]` range [`0-255`] or hex color string `#rrggbbaa`) 9 | - new builtin function: `arcTo` like [WebKit Context2D arcTo](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arcTo) 10 | - new builtin function: `fontSize(size)` to set the current font height 11 | - mew builtin function: `star` to draw a star polygon 12 | - mew builtin function: `fillAndStroke` to fill and stroke in one shot 13 | - new builtin function: `imageGet` to load a PNG image form local file system 14 | - new builtin function: `imageAt` to draw a PNG image at specified location on the main canvas 15 | - new builtin function: `transform(x, y)` multiplies the point _x_, _y_ by the current matrix, returning a transformed position 16 | 17 | BREAKING CHANGES: 18 | 19 | - huge refactoring - GraphicContext is an interface, so, potentially we could use different drivers (PDF, SVG, etc.) 20 | - REPL removed (now `g2d` works only as commandline tool) 21 | - ~~`worldcoors(...)`~~ renamed to `viewport(...)` 22 | - ~~`pensize(...)`~~ renamed to `strokeWeight(...)` 23 | 24 | BUG FIXES: 25 | 26 | - closes issues #1 #2 and #3 27 | 28 | --- 29 | 30 | 0.4.1 (Dec 15, 2020) 31 | 32 | BUG FIXES: 33 | 34 | - fix gorelaser build 35 | 36 | --- 37 | 38 | 0.4.0 (Dec 15, 2020) 39 | 40 | NEW FEATURES: 41 | 42 | - new object type: `Image` 43 | - new builtin canvas functions: `width()`, `height()` to query canvas size at runtime 44 | - new builtin canvas functions: `scale(sx, sy)`, `identity()` as matrix transformations 45 | - new builtin canvas function: `loadPNG(filename)` to load an external PNG image 46 | - new builtin canvas function: `image(im, x, y, ax, ay)` to draw a loaded PNG image 47 | - new builtin canvas functions: `arcTo(x1, y1, x2, y2)` to draw a circular arc on path 48 | - new builtin canvas functions: `rect(x, y, w, h, tl, tr, br, bl)` to draw a rectangle with different rounded angles 49 | - new builtin math function: `map(value, start1, stop1, start2, stop2)` to re-map a number from one range to another 50 | - new builtin math function: `lerp(start, stop, amt)` to calculate a number between two numbers at a specific increment 51 | - new builtin math function: `min(n1, n2, ...n)` to calculate the minimum of a sequence 52 | - new builtin math function: `max(n1, n2, ...n)` to calculate the maximum of a sequence 53 | 54 | 55 | BREAKING CHANGES: 56 | 57 | - massive code refactory 58 | - renamed `screensize` function to `size` 59 | - renamed `rectangle` function to `rect` 60 | - renamed `saveState` function to `push` 61 | - renamed `restoreState` function to `pop` 62 | 63 | --- 64 | 65 | 0.3.0 (Dec 9, 2020) 66 | 67 | NEW FEATURES: 68 | 69 | - canvas can be either square or rectangular; use `screensize(W,H)` 70 | - new builtin math functions: `radians(angle)` and `degrees(angle)` for angles conversion 71 | - new builtin canvas function: `fontsize([size])` to get or set the current font size 72 | - new builtin canvas function: `text(str, x, y, ax, ay)` to write text on canvas 73 | - new examples: spirograph, hypocycloid showing the use of custom functions 74 | - new example: landscape to show howt to create not only squared images 75 | 76 | BUG FIXES: 77 | 78 | - Fix redundand rad->degrees conversion in `arc(...)` and `ellArc(...)` functions 79 | 80 | CHANGES: 81 | 82 | - `polygon(...)` builtin function now takes angles in radians 83 | - `arc(...)` builtin function now takes angles in radians 84 | - `ellArc(...)` builtin function now takes angles in radians 85 | - `rotate(...)` builtin function now takes angles in radians 86 | 87 | --- 88 | 89 | 0.2.0 (Dec 7, 2020) 90 | 91 | NEW FEATURES: 92 | 93 | - better comparing between integers and floats 94 | - add new examples: dots, heart showing the use of custom function 95 | 96 | BUG FIXES: 97 | 98 | - ensure comparison between integers and floats in while loops works 99 | 100 | --- 101 | 102 | 0.1.0 (Dec 4, 2020) 103 | 104 | First release -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ![](./logo.png) 3 | 4 | [![Go Report Card](https://goreportcard.com/badge/github.com/lucasepe/g2d)](https://goreportcard.com/report/github.com/lucasepe/g2d)     [![Go Coverage](https://gocover.io/_badge/github.com/lucasepe/g2d?nocache=g2d)](https://gocover.io/_badge/github.com/lucasepe/g2d?nocache=g2d) 5 | 6 | 7 | # `g2D` - Geometry art built coding 8 | 9 | Use the code basics like loops, control flow and specialized functions to generate your geometry artworks. 10 | 11 | ## Examples of g2D Geometry Art 12 | 13 | - source code in [_examples](./_examples) folder 14 | 15 | ![](./_examples/heart.png)   ![](./_examples/circles.png)   ![](./_examples/maurer.png) 16 | 17 | ![](./_examples/targets.png)   ![](./_examples/lines.png)   ![](./_examples/spiral.png) 18 | 19 | --- 20 | 21 | ## Installation 22 | 23 | 24 | ### [Go](https://golang.org/doc/install) get 25 | 26 | ```bash 27 | $ go get -u github.com/lucasepe/g2d 28 | ``` 29 | 30 | ## Ready-To-Use Releases 31 | 32 | [Here you can find `g2d` already compiled](https://github.com/lucasepe/g2d/releases/latest) for: 33 | 34 | - MacOS 35 | - Linux 36 | - Windows 37 | 38 | --- 39 | 40 | ## How to use 41 | 42 | To execute a local `g2d` script: 43 | 44 | ```bash 45 | $ g2d /path/to/my-script.g2d 46 | ``` 47 | 48 | To execute a `g2d` script stored somewhere on the web: 49 | 50 | ```bash 51 | $ g2d http://my-cool-site.com/remote/path/to/my-script.g2d 52 | ``` 53 | 54 | Use the `--directory` (or the shorter `-d`) flag to specify a destination folder for the generated PNG images. 55 | 56 | --- 57 | 58 | 59 | ## The `g2D` Programming Language Syntax 60 | 61 | Example-programs can be found beneath [examples/](examples/) which demonstrate these things, as well as parts of the standard-library. 62 | 63 | ### Types 64 | 65 | g2D has the following data types: `bool`, `int`, `float`, `str`, `array`, and `fn`. 66 | 67 | Type | Syntax | Notes | 68 | --------- | ----------------------------------------- | ----------------------------------------------- | 69 | bool | `true false` | | 70 | int | `0 42 1234 -5` | is a signed 64-bit integer | 71 | float | `0.5 4.2 1.234 -5.5` | is a 64-bit double-precision floating point | 72 | str | `"" "foo" "\"quotes\" and a\nline break"` | are immutable arrays of bytes | 73 | array | `[] [1, 2] [1, 2, 3]` | grow-able arrays (*use the `append()` builtin*) | 74 | fn | `fn(a, b) { ... }` | defines a custom function | 75 | 76 | 77 | ### Bindings 78 | 79 | Variables are bounded using the `:=` operator. 80 | 81 | ```go 82 | a := 3 83 | b := 1.2 84 | ``` 85 | 86 | Variables may be integers, floats, strings, or arrays/hashes. 87 | 88 | To update a variable you can simply specify the equals `=` operator: 89 | 90 | ```go 91 | a := 3 // Binding 92 | a = a + 5 // Updating 93 | ``` 94 | 95 | ### Arithmetic operations 96 | 97 | `g2D` supports all the basic arithmetic operation of `int` and `float` types. 98 | 99 | ```go 100 | a := 5 101 | b := 3 102 | 103 | c := a + b 104 | d := c / 2 105 | e := d * d 106 | ``` 107 | 108 | ### Builtin containers 109 | 110 | `g2d` has one builtin containers: `array`. 111 | 112 | #### Arrays 113 | 114 | An array is a list which organizes items by linear sequence. Arrays can hold multiple types. 115 | 116 | ```go 117 | a := [1, 2.3, "hello!"] 118 | b := [false, true, "Hello World", 3, 3.13] 119 | ``` 120 | 121 | Adding to an array is done via the `push` builtin function: 122 | 123 | ```go 124 | a = append(a, "another") 125 | ``` 126 | 127 | You can iterate over the contents of an array like so: 128 | 129 | ```go 130 | i := 0 131 | while( i < len(a) ) { 132 | print( "Array index ", i, " contains ", a[i], "\n") 133 | i = i + 1 134 | } 135 | ``` 136 | 137 | With the definition we included that produces this output: 138 | 139 | ```text 140 | Array index 0 contains 1 141 | Array index 1 contains 2.3 142 | Array index 2 contains hello! 143 | Array index 3 contains another 144 | ``` 145 | 146 | ### Functions 147 | 148 | `g2D` uses `fn` to define a function which will be assigned to a variable for naming/invocation purposes: 149 | 150 | ```go 151 | sum := fn(a, b) { return a + b } 152 | 153 | print(sum(5,3), "\n") // Outputs: 8 154 | print(sum(2.5,7.5), "\n") // Outputs: 10 155 | ``` 156 | 157 | Functions can be passed as values to others functions: 158 | 159 | ```go 160 | addTwo := fn(a, b, f) { 161 | return 2 + f(a, b) 162 | } 163 | 164 | tot := addTwo(68, 1, sum) 165 | print(tot, "\n") // Outputs: 71 166 | ``` 167 | 168 | Functions inside functions 169 | 170 | ```go 171 | multiplier := fn(q) { 172 | return fn(x) { 173 | return x*q 174 | } 175 | } 176 | 177 | multThree := multiplier(3) 178 | 179 | print(multThree(2), "\n") // Outputs: 6 180 | print(multThree(3), "\n") // Outputs: 9 181 | print(multThree(4), "\n") // Outputs: 12 182 | ``` 183 | 184 | ### If-else statements 185 | 186 | `g2D` supports `if-else` statements. 187 | 188 | ```go 189 | max := fn(a, b) { 190 | if (a > b) { 191 | return a; 192 | } else { 193 | return b; 194 | } 195 | } 196 | 197 | print( max(1, 2) ) // Outputs: 2 198 | ``` 199 | 200 | ### Switch Statements 201 | 202 | `g2D` supports the `switch` and `case` expressions: 203 | 204 | ```go 205 | switch n := randi(10) { 206 | case n % 2 == 0 { 207 | print(n, " is even", "\n") 208 | } 209 | default { 210 | print(n, " is odd", "\n") 211 | } 212 | } 213 | ``` 214 | 215 | ### While Loops 216 | 217 | `g2D` supports only one looping construct, the `while` loop: 218 | 219 | ```go 220 | i := 30 221 | while (i > 0) { 222 | print(i, " ") 223 | i = i - 10 224 | } 225 | // 30 20 10 226 | ``` 227 | 228 | --- 229 | 230 | ## Builtin functions 231 | 232 | ### Core 233 | 234 | Function | Description 235 | ---------------------- | -------------------------------------------------------------------------- | 236 | `exit([status])` | exits the program immediately with the optional status or 0 | 237 | `input([prompt]` | reads a line from standard input optionally printing the specified prompt | 238 | `print(...)` | output a string to stdout | 239 | `printf(pattern, ...)` | output a string to stdout (formatted according the specified pattern) | 240 | `sprintf(pattern, ...)`| like `printf(...)` but returns a _string_ | 241 | `bool(val)` | converts value to a bool | 242 | `float(val)` | converts decimal value str to _float_ - if _val_ is invalid returns _null_ | 243 | `int(val)` | converts decimal value str to _int_ - if _val_ is invalid returns _null_ | 244 | `str(val)` | returns the string representation of _val_ | 245 | `len(iterable)` | returns the length of the iterable (_string_ or _array_) | 246 | `append(array, val)` | returns a new array with value pushed onto the end of array | 247 | 248 | 249 | ### Calculation 250 | 251 | Function | Description 252 | ----------------------- | -------------------------------------------------------------------------- | 253 | `abs(x)` | returns the absolute value of _x_ | 254 | `atan(x)` | returns the arc tangent, in radians, of _x_ | 255 | `atan2(x, y)` | returns the arc tangent of _y/x_ | 256 | `cos(x)` | returns the cosine of the radian argument _x_ | 257 | `degrees(angle)` | converts radians into degrees | 258 | `hypot(p, q)` | returns `sqrt(p*p + q*q)` | 259 | `lerp(start, stop, amt)`| calculates a number between two numbers at a specific increment | 260 | `map(v, b1, e1, b2, e2)`| re-maps a number from one range to another | 261 | `max(v1....vn)` | returns the largest value in a sequence of numbers | 262 | `min(v1....vn)` | returns the smallest value in a sequence of numbers | 263 | `pow(x, y)` | returns `x**y`, the base _x_ exponential of _y_ | 264 | `sin(x)` | returns the sine of the radian argument _x_ | 265 | `sqrt(x)` | returns the square root of _x_ | 266 | `radians(angle)` | converts a degree measurement to its corresponding value in radians | 267 | `randf([min], [max])` | returns a random float between min and max - by default min=0.0 and max=1.0| 268 | `randi([min], [max])` | returns a random int between min and max | 269 | 270 | ### Basic graphic functions 271 | 272 | Function | Description 273 | ------------------------------------- | ------------------------------------------------------------------------------------- | 274 | `size(w,[h])` | sets the size of the drawing; when both _w_ and _h_ are specified creates a rectangular image otherwise creates a squared one | 275 | `viewport(xMin, xMax, yMin, yMax, xOffset, yOffset)` | sets up user-defined coordinate system; performs a screen reset (drawings are cleared)| 276 | `clear()` | fills the entire image with the current color; clear all drawings | 277 | `fillColor(hexcolor)` | sets the fill color to the specified _hexcolor_; example _fillColor("#ff0000")_ | 278 | `fillColor(r, g, b, [a])` | sets the fill color to _r,g,b,a_ values - should be between 0 and 255, inclusive | 279 | `strokeColor(hexcolor)` | sets the stroke color to the specified _hexcolor_; example _strokeColor("#ff0000")_ | 280 | `strokeColor(r, g, b, [a])` | sets the stroke color to _r,g,b,a_ values - should be between 0 and 255, inclusive | 281 | `strokeWeight(weight)` | sets the stroke thickness to the specified _width_ | 282 | `dashes([s1, s2, ...sn])` | sets the current dash pattern to use (call with zero arguments to disable dashes) | 283 | `stroke()` | strokes the current path with the current stroek color and line width the path is cleared after this operation | 284 | `fill()` | fills the current path with the current fill color; open subpaths are implicity closed.
The path is cleared after this operation | 285 | `fillAndStroke()` | fills the current path with the current fill color and strokes it with the current stroke color; the path is cleared after this operation | 286 | `push()` | saves the current state of the graphic context by pushing it onto a stack | 287 | `pop()` | restores the last saved graphic context state from the stack | 288 | `snapshot([filename])` | creates a PNG image with the current drawings.
If _filename_ is omitted, it will be autogenerated with a progressive counter, that will be incremented on each
`snapshot()` invocation; this is useful if you wants to generate an animation later (using all the generated PNG images). | 289 | `xpos()` | returns the current X position (if there is a current point) | 290 | `ypos()` | returns the current Y position (if there is a current point) | 291 | 292 | ### Graphic primitives 293 | 294 | Function | Description 295 | ------------------------------------- | ------------------------------------------------------------------------------------- | 296 | `arc(x, y, r, sa, ea)` | draws a circular arc centered at _(x, y)_ with a radius of _r_.
The path starts at _sa_ angle_, ends at _ea_ angle, and travels in the direction given by anticlockwise | 297 | `circle(x, y, r)` | draws a circle centered at _[x, y]_ coordinates and with the radius _r_ | 298 | `ellipse(x, y, rx ,ry)` | draws an ellipse centered at [x, y] coordinates and with the radii _rx_ and _ry_ | 299 | `line(x1, y1, x2, y2)` | draws a line from point _(x1, y1)_ to point _(x2, y2)_ | 300 | `point(x, y)` | draws a point at specified coordinates (the size is equal to the stroke weight) | 301 | `quad(x1, y1, x2,y2, x3,y3, x4,y4)` | draws a a four sided polygon using the provided vertices | 302 | `rect(x, y, w, h, [tl, tr, br, bl])` | draws a (w x h) rectangle with upper left corner located at _(x, y)_.
If only one radius is specified, all the corners have the same bending, if _tl_, _tr_, _br_, _bl_ are specified, each corner can have a different curvature | 303 | `triangle(x1,y1, x2,y2, x3,y3)` | draws a triangle using the provided vertices | 304 | `star(cx, cy, n, or, ir)` | draws a star _cx_, _cy_ is the center, _n_ the number of spikes, _or_ and _ir_ the outer and inner radius | 305 | 306 | ### Paths 307 | 308 | Function | Description 309 | ------------------------------------- | -------------------------------------------------------------------------------------- | 310 | `beginPath()` | starts a new path | 311 | `closePath()` | adds a line segment from the current point to the beginning of the current subpath | 312 | `moveTo(x, y)` | sets the begin of a new subpath starting at the specified _x_, _y_ point | 313 | `lineTo(x, y)` | adds a line segment to the current path starting at the current point | 314 | `arcTo(x1, y1, x2, y2, r)` | adds a circular arc to the current sub-path, using the given control points and radius | 315 | `quadraticCurveTo(x1, y1, x2, y2)` | adds a quadratic Bézier curve to the current sub-path; _x1_, _y1_ is the control point and _x2_, _y2_ is the end point | 316 | 317 | ### Transform 318 | 319 | Function | Description 320 | ------------------------------------- | -------------------------------------------------------------------------------------- | 321 | `rotate(angle, [x, y] )` | updates the current matrix with a anticlockwise rotation; when _x, y_ are specified, rotation occurs about this point, otherwise rotation occurs about the origin (_angle_ is in radians) | 322 | `scale(sx, sy, [x, y])` | updates the current matrix with _sx_, _sy_ scaling factor; when _x_,_y_ are specified, scaling occurs about this point, otherwise scaling occurs about origin. | 323 | `translate(x, y)` | updates the current matrix with a translation to _x_ and _y_ | 324 | `identity()` | resets the current transformation matrix to the identity matrix | 325 | `transform(x, y)` | multiplies the point _x_, _y_ by the current matrix, returning a transformed position | 326 | 327 | ### Text 328 | 329 | Function | Description 330 | ------------------------------------- | -------------------------------------------------------------------------------------- | 331 | `text(str, x, y, [ax, ay])` | draws the specified text _str_ at the specified anchor point _x_, _y_; the anchor point is _x - w * ax_, _y - h * ay_, where _w_, _h_ is the size of the text (by default _ax=0.5_, _ay=0.5_ to center the text at the specified point) | 332 | `textWidth(str)` | returns the rendered width of the specified text _str_ given the current font face | 333 | `fontSize(size)` | sets the font height | 334 | 335 | ### Images 336 | 337 | Function | Description 338 | ------------------------------------- | -------------------------------------------------------------------------------------- | 339 | `imageGet(path/to/png)` | loads a PNG image from the local filesystem | 340 | `imageAt(im, x, y, [ax, ay])` | draws the specified image _im_ at the specified anchor point _x_, _y_; (_ax_ and _ay_ are the x and y offsets) use ax=0.5, ay=0.5 to center the image at the specified point | 341 | -------------------------------------------------------------------------------- /_examples/Makefile: -------------------------------------------------------------------------------- 1 | CC := go run ../main.go 2 | CFLAGS := eval -d . 3 | 4 | SOURCES ?= $(wildcard *.g2d) 5 | 6 | .PHONY: all clean 7 | 8 | # pattern rule to parse all .g2d files and generate .png 9 | %.png: %.g2d 10 | $(CC) $(CFLAGS) $< 11 | 12 | # rule to make a .png to each .g2d 13 | all: clean $(SOURCES:%.g2d=%.png) 14 | 15 | clean: 16 | rm -f *.png -------------------------------------------------------------------------------- /_examples/arc.g2d: -------------------------------------------------------------------------------- 1 | size(600, 400) 2 | 3 | fillColor(255, 255, 255) 4 | clear() 5 | 6 | rows := 2 7 | cols := 3 8 | 9 | dx := WIDTH / cols 10 | dy := HEIGHT / rows 11 | 12 | fontSize(20) 13 | 14 | strokeColor(0, 0, 0) 15 | fillColor(255, 238, 0, 250) 16 | strokeWeight(4) 17 | 18 | r := 0.35 * min(dx, dy - 2*fontSize()) 19 | 20 | 21 | i := 0 22 | while(i < cols) { 23 | j := 0 24 | while(j < rows) { 25 | cx := 0.5*dx + i*dx 26 | cy := 0.5*dy + j*dy - fontSize() 27 | 28 | sa := randi(0, 90) 29 | ea := sa + randi(90, 270) 30 | 31 | arc(cx, cy, 0.8*r, radians(sa), radians(ea)) 32 | stroke() 33 | 34 | push() 35 | strokeWeight(1) 36 | dashes(0.2*r, 0.1*r) 37 | line(cx, cy-r, cx, cy+r) 38 | line(cx-r, cy, cx+r, cy) 39 | stroke() 40 | pop() 41 | 42 | y := cy + 1.2*r 43 | text(sprintf("sa = %d°", sa), cx, y) 44 | text(sprintf("ea = %d°", ea), cx, y + 1.1*fontSize()) 45 | 46 | j = j + 1 47 | 48 | push() 49 | line(0, j*dy, WIDTH, j*dy) 50 | strokeColor(205, 202, 202, 202) 51 | stroke() 52 | pop() 53 | } 54 | 55 | i = i + 1 56 | 57 | push() 58 | line(i*dx, 0, i*dx, HEIGHT) 59 | strokeColor(205, 202, 202, 202) 60 | stroke() 61 | pop() 62 | } 63 | 64 | //fontSize(24) 65 | //text("sa = 20", cx, cy) 66 | 67 | snapshot("arc.png") -------------------------------------------------------------------------------- /_examples/arc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/arc.png -------------------------------------------------------------------------------- /_examples/arrows.g2d: -------------------------------------------------------------------------------- 1 | 2 | arrow := fn(x1, y1, x2, y2, size) { 3 | dx := x2 - x1 4 | dy := y2 - y1 5 | 6 | angle := atan2(dy, dx) 7 | dist := hypot(dx, dy) 8 | 9 | push() 10 | translate(x1, y1) 11 | rotate(angle) 12 | 13 | // line 14 | moveTo(0, 0) 15 | lineTo(dist - size, 0) 16 | stroke() 17 | 18 | // triangle 19 | moveTo(dist - size, size) 20 | lineTo(dist, 0) 21 | lineTo(dist - size, -size) 22 | fill() 23 | 24 | pop() 25 | } 26 | 27 | size(128) 28 | 29 | l := WIDTH / 2 30 | 31 | strokeWeight(4) 32 | strokeColor("#ff0000") 33 | fillColor("#ff0000") 34 | arrow(l, l, 2*l, l, 10) 35 | 36 | strokeColor("#00ff00") 37 | fillColor("#00ff00") 38 | arrow(l, l, l, 2*l, 10) 39 | 40 | snapshot("arrows.png") 41 | -------------------------------------------------------------------------------- /_examples/arrows.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/arrows.png -------------------------------------------------------------------------------- /_examples/circles.g2d: -------------------------------------------------------------------------------- 1 | pointInCircle := fn(radius) { 2 | r := radius * sqrt(randf()) 3 | theta := randf() * 2 * PI 4 | x := r * cos(theta) 5 | y := r * sin(theta) 6 | 7 | return [x, y] 8 | } 9 | 10 | // Creates an image that is 480 x 120 pixels 11 | size(256) 12 | // Clear the image 13 | 14 | clear() 15 | 16 | viewport(-105, 105, -105, 105) 17 | 18 | // Do iterations 19 | i := 0 20 | while(i < 200) { 21 | // circle radius 22 | r := randf(5, 50) 23 | 24 | // circle center 25 | c := pointInCircle(r) 26 | 27 | // draws the circle 28 | circle(c[0], c[1], r) 29 | 30 | // generate a random integer... 31 | // if it is even 32 | if randi() % 2 == 0 { 33 | // fill the circle with a black color and a random transparency 34 | fillColor(randi(255), randi(255), randi(255), randi(50, 150)) 35 | fill() 36 | } else { 37 | //...if it's odd, outline the circle 38 | strokeColor(randi(0, 20), randi(0, 20), randi(0, 20), randi(50, 150)) 39 | // Sets the pen size 40 | strokeWeight(randf(1, 6)) 41 | stroke() 42 | } 43 | // increment the counter 44 | i = i + 1 45 | } 46 | 47 | // save the image 48 | snapshot("circles.png") 49 | -------------------------------------------------------------------------------- /_examples/circles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/circles.png -------------------------------------------------------------------------------- /_examples/ellipse.g2d: -------------------------------------------------------------------------------- 1 | size(600, 400) 2 | 3 | fillColor(255, 255, 255) 4 | clear() 5 | 6 | rows := 2 7 | cols := 3 8 | 9 | dx := WIDTH / cols 10 | dy := HEIGHT / rows 11 | 12 | fontSize(20) 13 | 14 | strokeColor(0, 0, 0) 15 | fillColor(255, 238, 0, 250) 16 | strokeWeight(4) 17 | 18 | lim := 0.35 * min(dx, dy - 2*fontSize()) 19 | 20 | 21 | i := 0 22 | while(i < rows) { 23 | 24 | j := 0 25 | while(j < cols) { 26 | cx := 0.5*dx + j*dx 27 | cy := 0.5*dy + i*dy 28 | 29 | rx := randf(0.6*lim, 0.9*lim) 30 | ry := randf(0.4*lim, 0.8*lim) 31 | 32 | ellipse(cx, cy, rx, ry) 33 | stroke() 34 | 35 | y := cy + 0.5*dy - 2*fontSize() 36 | text(sprintf("rx = %.2f", rx), cx, y) 37 | text(sprintf("ry = %.2f", ry), cx, y + fontSize()) 38 | 39 | push() 40 | strokeWeight(1) 41 | dashes(0.2*lim, 0.1*lim) 42 | line(cx, cy-lim, cx, cy+lim) 43 | line(cx-lim, cy, cx+lim, cy) 44 | stroke() 45 | pop() 46 | 47 | j = j + 1 48 | 49 | push() 50 | line(0, j*dy, WIDTH, j*dy) 51 | strokeColor(205, 202, 202, 202) 52 | stroke() 53 | pop() 54 | } 55 | 56 | i = i + 1 57 | 58 | push() 59 | line(i*dx, 0, i*dx, HEIGHT) 60 | strokeColor(205, 202, 202, 202) 61 | stroke() 62 | pop() 63 | } 64 | 65 | 66 | snapshot("ellipse.png") -------------------------------------------------------------------------------- /_examples/ellipse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/ellipse.png -------------------------------------------------------------------------------- /_examples/grid.g2d: -------------------------------------------------------------------------------- 1 | // Image size 2 | size(600, 400) 3 | // White background 4 | fillColor(255, 255, 255) 5 | clear() 6 | 7 | // Line color and thickness 8 | strokeColor(205, 202, 202, 202) 9 | strokeWeight(4) 10 | 11 | // Number of rows and columns 12 | rows := 3 13 | cols := 3 14 | 15 | // Cell size 16 | dx := WIDTH / cols 17 | dy := HEIGHT / rows 18 | 19 | // Vertical lines 20 | j := 1 21 | while(j < cols) { 22 | line(j*dx, 0, j*dx, HEIGHT) 23 | stroke() 24 | 25 | j = j + 1 26 | } 27 | 28 | // Horizontal lines 29 | i := 1 30 | while(i < rows) { 31 | line(0, i*dy, WIDTH, i*dy) 32 | stroke() 33 | 34 | i = i + 1 35 | } 36 | 37 | fillColor(0, 0, 0, 202) 38 | 39 | // Iterate over the range [0, rows*cols] 40 | i := 0 41 | while(i < rows*cols) { 42 | // Find row and column index 43 | r := i / rows 44 | c := i % rows 45 | // Find the center of the cell 46 | cx := 0.5*dx + c*dx 47 | cy := 0.5*dy + r*dy 48 | // Draws a point 49 | point(cx, cy) 50 | fill() 51 | 52 | i = i + 1 53 | } 54 | 55 | // Save the image 56 | snapshot("grid.png") -------------------------------------------------------------------------------- /_examples/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/grid.png -------------------------------------------------------------------------------- /_examples/heart.g2d: -------------------------------------------------------------------------------- 1 | // Formula: https://mathworld.wolfram.com/HeartCurve.html 2 | heartShape := fn(x0, y0, r) { 3 | moveTo(x0, y0) 4 | 5 | t := 0 6 | while(t < 360) { 7 | tr := radians(t) 8 | x := r * 16 * pow(sin(tr), 3) 9 | y := r * (13 * cos(tr) - 5 * cos(2 * tr) - 2 * cos(3 * tr) - cos(4 * tr)) 10 | 11 | lineTo(x, y) 12 | t = t + 1 13 | } 14 | } 15 | 16 | // Picture size 17 | size(256) 18 | clear() 19 | 20 | // Custom coordinates system 21 | viewport(-70, 70, -70, 70) 22 | 23 | // Draw the Heart shape few times 24 | r := 4 25 | while(r > 1) { 26 | heartShape(0, 0, r) 27 | fillColor(randi(150, 250), randi(0, 160), randi(0, 150), randi(200, 250)) 28 | fill() 29 | 30 | r = r - 0.5 31 | } 32 | 33 | // Saves the output 34 | snapshot("heart.png") -------------------------------------------------------------------------------- /_examples/heart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/heart.png -------------------------------------------------------------------------------- /_examples/lines.g2d: -------------------------------------------------------------------------------- 1 | size(256) 2 | clear() 3 | 4 | viewport(-WIDTH/2, WIDTH/2,-HEIGHT/2, HEIGHT/2) 5 | 6 | i := 0 7 | while (i < 200) { 8 | x1 := randf(-WIDTH/2, WIDTH/2) 9 | y1 := randf(-HEIGHT/2, HEIGHT/2) 10 | 11 | x2 := 10 + randf(-WIDTH/2, WIDTH/2) 12 | y2 := 10 + randf(-HEIGHT/2, HEIGHT/2) 13 | 14 | line(x1, y1, x2, y2) 15 | 16 | strokeWeight(randf(1, 5)) 17 | strokeColor(randf(255), randf(255), randf(255), randf(150, 255)) 18 | stroke() 19 | 20 | i = i + 1 21 | } 22 | 23 | snapshot("lines.png") -------------------------------------------------------------------------------- /_examples/lines.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/lines.png -------------------------------------------------------------------------------- /_examples/logo.g2d: -------------------------------------------------------------------------------- 1 | // Image size 2 | size(128) 3 | // Transparent background 4 | clear() 5 | 6 | viewport(-1.0, 1.0, -1.0, 1.0) 7 | 8 | // Head (upper) 9 | quad(-0.8, 0, -0.3, 0.6, 0.3, 0.6, 0.8, 0) 10 | fillColor("#af2206") 11 | strokeColor("#371008") 12 | strokeWeight(4) 13 | fillAndStroke() 14 | // Head (lower) 15 | triangle(-0.8, 0, 0, -0.8, 0.8, 0) 16 | fillColor("#891903") 17 | strokeColor("#371008") 18 | fillAndStroke() 19 | 20 | // Eye (big) 21 | circle(-0.2, 0, 0.3) 22 | // Eye (small) 23 | circle(0.3, 0, 0.1) 24 | fillColor("#fafafa") 25 | strokeColor("#a4abab") 26 | fillAndStroke() 27 | 28 | // Iris (big eye) 29 | circle(-0.2, 0, 0.15) 30 | fillColor("#fef71b") 31 | strokeColor("#d7d107") 32 | fillAndStroke() 33 | 34 | 35 | p := transform(0.3, lerp(0, -0.8, 0.5)) 36 | 37 | identity() 38 | rotate(radians(-45), p[0], p[1]) 39 | 40 | fs := max(0.04 * min(WIDTH, HEIGHT), 18) 41 | 42 | strokeColor("#fafafa") 43 | fontSize(fs) 44 | text("g2D", p[0], p[1], 0.5, -0.1) 45 | 46 | // save the image 47 | snapshot("logo.png") -------------------------------------------------------------------------------- /_examples/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/logo.png -------------------------------------------------------------------------------- /_examples/maurer.g2d: -------------------------------------------------------------------------------- 1 | // https://en.wikipedia.org/wiki/Maurer_rose 2 | 3 | size(256) 4 | clear() 5 | 6 | viewport(-140, 140, -140, 140) 7 | 8 | n := 6 9 | d := 71 10 | S := min(WIDTH, HEIGHT) 11 | 12 | 13 | theta := 0 14 | while (theta <= 360) { 15 | k := d * radians(theta) 16 | r := 0.5 * S * sin(n * k) 17 | x := -r * cos(k) 18 | y := -r * sin(k) 19 | 20 | lineTo(x, y) 21 | moveTo(x, y) 22 | 23 | theta = theta + 1 24 | } 25 | strokeColor("#0000ff") 26 | strokeWeight(0.5) 27 | stroke() 28 | 29 | theta := 0 30 | while (theta <= 360) { 31 | k := radians(theta) 32 | r := 0.5 * S * sin(n * k) 33 | x := r * cos(k) 34 | y := -r * sin(k) 35 | 36 | lineTo(x, y) 37 | moveTo(x, y) 38 | 39 | theta = theta + 1 40 | } 41 | strokeColor("#ff0000") 42 | strokeWeight(2) 43 | stroke() 44 | 45 | snapshot("maurer.png") -------------------------------------------------------------------------------- /_examples/maurer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/maurer.png -------------------------------------------------------------------------------- /_examples/none.g2d: -------------------------------------------------------------------------------- 1 | print( #comment 2 | "Hello, World\n"); -------------------------------------------------------------------------------- /_examples/rect.g2d: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | 3 | fillColor(255, 255, 255) 4 | clear() 5 | 6 | rows := 3 7 | cols := 3 8 | 9 | dx := WIDTH / cols 10 | dy := HEIGHT / rows 11 | 12 | fontSize(20) 13 | 14 | strokeColor(0, 0, 0) 15 | fillColor(255, 238, 0, 250) 16 | strokeWeight(4) 17 | 18 | r := 0.35 * min(dx, dy - 2*fontSize()) 19 | 20 | 21 | i := 0 22 | while(i < rows) { 23 | 24 | j := 0 25 | while(j < cols) { 26 | cx := 0.5*dx + j*dx 27 | cy := 0.5*dy + i*dy 28 | 29 | rr := 0.9*r 30 | 31 | if i == 0 { 32 | t := int(j*randf(0.45*rr)) 33 | rect(cx-rr, cy-rr, 2*rr, 2*rr, t) 34 | stroke() 35 | 36 | y := cy + 1.2*r 37 | text(sprintf("r = %d", t), cx, y) 38 | } else { 39 | tl := int(randf(0.45*rr)) 40 | tr := int(randf(0.45*rr)) 41 | br := int(randf(0.45*rr)) 42 | bl := int(randf(0.45*rr)) 43 | rect(cx-rr, cy-rr, 2*rr, 2*rr, tl, tr, br, bl) 44 | stroke() 45 | 46 | y := cy + 1.2*r 47 | text(sprintf("r = %d,%d,%d,%d", tl, tr, br, bl), cx, y) 48 | } 49 | 50 | j = j + 1 51 | 52 | push() 53 | line(0, j*dy, WIDTH, j*dy) 54 | strokeColor(205, 202, 202, 202) 55 | stroke() 56 | pop() 57 | } 58 | 59 | i = i + 1 60 | 61 | push() 62 | line(i*dx, 0, i*dx, HEIGHT) 63 | strokeColor(205, 202, 202, 202) 64 | stroke() 65 | pop() 66 | } 67 | 68 | //fontSize(24) 69 | //text("sa = 20", cx, cy) 70 | 71 | snapshot("rect.png") -------------------------------------------------------------------------------- /_examples/rect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/rect.png -------------------------------------------------------------------------------- /_examples/sierpinsky.g2d: -------------------------------------------------------------------------------- 1 | // Draw triangle function 2 | triangle := fn(p0, p1, p2) { 3 | push() 4 | moveTo(p0[0], p0[1]) 5 | lineTo(p1[0], p1[1]) 6 | lineTo(p2[0], p2[1]) 7 | closePath() 8 | pop() 9 | } 10 | 11 | // Draw Sierpinsky funcion 12 | sierpinski := fn(p0, p1, p2, limit) { 13 | if limit > 0 { 14 | // Search the middle point of every line of the triangle 15 | xA := (p1[0] + p2[0]) / 2 16 | yA := (p1[1] + p2[1]) / 2 17 | pA := [xA, yA] 18 | 19 | xB := (p0[0] + p2[0]) / 2 20 | yB := (p0[1] + p2[1]) / 2 21 | pB := [xB, yB] 22 | 23 | xC := (p0[0] + p1[0]) / 2 24 | yC := (p0[1] + p1[1]) / 2 25 | pC := [xC, yC] 26 | 27 | // Calls the sierpinski function recursively 28 | // Until limit is reached 29 | sierpinski(p0, pB, pC, limit - 1) 30 | sierpinski(pC, pA, p1, limit - 1) 31 | sierpinski(pB, pA, p2, limit - 1) 32 | } else { 33 | triangle(p0, p1, p2) 34 | } 35 | } 36 | 37 | size(256) 38 | clear() 39 | 40 | viewport(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2) 41 | 42 | // Triangle side length 43 | side := 0.9*min(WIDTH, HEIGHT) 44 | 45 | // Triangle Vertices 46 | pA := [-side/2, -side/2] 47 | pB := [side/2, -side/2] 48 | pC := [0, side/2] 49 | 50 | // How many recursions? 51 | deep := 4 52 | 53 | // Start iterations 54 | sierpinski(pA, pB, pC, deep) 55 | 56 | // Fill the shape 57 | fillColor("#0fd5a5") 58 | fill(true) 59 | 60 | // Stroke the lines 61 | strokeColor("#000000") 62 | strokeWeight(2) 63 | stroke() 64 | 65 | // Save the output 66 | snapshot("sierpinsky.png") -------------------------------------------------------------------------------- /_examples/sierpinsky.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/sierpinsky.png -------------------------------------------------------------------------------- /_examples/snowflake.g2d: -------------------------------------------------------------------------------- 1 | koch := fn(a, b, limit) { 2 | dx := b[0] - a[0] 3 | dy := b[1] - a[1] 4 | dist := hypot(dx, dy) 5 | unit := dist/3 6 | angle := atan2(dy, dx) 7 | 8 | x1 := a[0] + dx / 3 9 | y1 := a[1] + dy / 3 10 | p1 := [x1, y1] 11 | 12 | x2 := x1 + cos(angle - PI / 3) * unit 13 | y2 := y1 + sin(angle - PI / 3) * unit 14 | p2 := [x2, y2] 15 | 16 | x3 := b[0] - dx / 3 17 | y3 := b[1] - dy / 3 18 | p3 := [x3, y3] 19 | 20 | if limit > 0 { 21 | koch(a, p1, limit - 1) 22 | koch(p1, p2, limit - 1) 23 | koch(p2, p3, limit - 1) 24 | koch(p3, b, limit - 1) 25 | } else { 26 | push() 27 | //moveTo(a[0], a[1]) 28 | lineTo(x1, y1) 29 | lineTo(x2, y2) 30 | lineTo(x3, y3) 31 | lineTo(b[0], b[1]) 32 | pop() 33 | } 34 | } 35 | 36 | size(256) 37 | clear() 38 | 39 | viewport(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2) 40 | 41 | // Shape size 42 | side := 0.5*min(WIDTH, HEIGHT) 43 | 44 | // Shape Vertices 45 | pA := [-side/2, -side/2] 46 | pB := [side/2, -side/2] 47 | pC := [0, side/2] 48 | 49 | // How many recursions? 50 | deep := 3 51 | 52 | koch(pA, pB, deep) 53 | koch(pB, pC, deep) 54 | koch(pC, pA, deep) 55 | 56 | 57 | fillColor("#00ff00CC") 58 | strokeColor("#000000") 59 | strokeWeight(2) 60 | fillAndStroke() 61 | 62 | // Save the output 63 | snapshot("snowflake.png") -------------------------------------------------------------------------------- /_examples/snowflake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/snowflake.png -------------------------------------------------------------------------------- /_examples/spectre.g2d: -------------------------------------------------------------------------------- 1 | drawSide := fn(color1, color2, color3) { 2 | S := min(WIDTH, HEIGHT) 3 | 4 | lineTo(-S/2, 0) 5 | lineTo(-S/5, -S/5) 6 | lineTo(0, 0) 7 | fillColor(color1) 8 | fill() 9 | 10 | lineTo(0, -S/2) 11 | lineTo(-S/5, -S/5) 12 | lineTo(0, 0) 13 | fillColor(color2) 14 | fill() 15 | 16 | // details 17 | if color3 != null { 18 | line(-S/5, -S/5, 0, 0) 19 | strokeColor(color3) 20 | strokeWeight(2) 21 | stroke() 22 | } 23 | } 24 | 25 | drawEye := fn(color) { 26 | S := min(WIDTH, HEIGHT) 27 | l := -S/9 28 | 29 | moveTo(-l, 0) 30 | lineTo(0, -l) 31 | lineTo(l, 0) 32 | lineTo(0, l) 33 | 34 | fillColor(color) 35 | fill() 36 | } 37 | 38 | size(256) 39 | 40 | translate(0.5*WIDTH, 0.5*HEIGHT) 41 | 42 | // draw a piece in the 4 quadrant 43 | drawSide("#4e78bf", "#082e70", null) 44 | 45 | // flip over the y-axis 46 | push() 47 | scale(-1, 1) 48 | drawSide("#4e78bf", "#4e78bf", "#082e70") 49 | pop() 50 | 51 | // flip over both axis 52 | push() 53 | scale(-1, -1) 54 | drawSide("#082e70", "#4e78bf", null) 55 | pop() 56 | 57 | // flip over the x-axis 58 | push() 59 | scale(1, -1) 60 | drawSide("#082e70", "#082e70", "#4e78bf") 61 | pop() 62 | 63 | drawEye("#fdff8c") 64 | push() 65 | scale(0.7, 0.7) 66 | drawEye("#1d338c") 67 | pop() 68 | 69 | push() 70 | scale(0.4, 0.4) 71 | drawEye("#f3f6d2") 72 | pop() 73 | 74 | 75 | // save the image 76 | snapshot("spectre.png") -------------------------------------------------------------------------------- /_examples/spectre.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/spectre.png -------------------------------------------------------------------------------- /_examples/spiral.g2d: -------------------------------------------------------------------------------- 1 | 2 | size(256) 3 | clear() 4 | 5 | viewport(-WIDTH/2, WIDTH/2,-HEIGHT/2, HEIGHT/2) 6 | 7 | N := 512 8 | S := min(WIDTH, HEIGHT) 9 | 10 | i := 0 11 | while (i < N) { 12 | t := float(i) / float(N) 13 | d := t*S*0.4 + 10 14 | a := t * PI * 2 * 20 15 | 16 | x := cos(a)*d 17 | y := sin(a)*d 18 | r := t * 5 19 | 20 | circle(x, y, r) 21 | fillColor(randf(190, 255), randf(190, 255), randf(100, 150)) 22 | fill() 23 | 24 | i = i + 1 25 | } 26 | 27 | 28 | snapshot("spiral.png") -------------------------------------------------------------------------------- /_examples/spiral.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/spiral.png -------------------------------------------------------------------------------- /_examples/star.g2d: -------------------------------------------------------------------------------- 1 | size(256) 2 | clear() 3 | 4 | star(WIDTH/2, HEIGHT/2, 18, 100, 40) 5 | fillColor(255,45,65) 6 | fillAndStroke() 7 | 8 | snapshot("star.png") 9 | -------------------------------------------------------------------------------- /_examples/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/star.png -------------------------------------------------------------------------------- /_examples/targets.g2d: -------------------------------------------------------------------------------- 1 | // A function that draws concentric circles 2 | target := fn(x, y, size, num) { 3 | gray := 255/num 4 | steps := size/num 5 | 6 | i := 0 7 | while(i < num) { 8 | c := int(55-i * gray) 9 | fillColor(c, c, c, randi(230, 255)) 10 | ellipse(x, y, size - i*steps, size - i*steps) 11 | fill() 12 | i = i + 1 13 | } 14 | } 15 | 16 | 17 | size(256) 18 | clear() 19 | 20 | rMax := 0.35 * min(WIDTH, HEIGHT) 21 | 22 | // call the target funcion three times 23 | target(WIDTH*0.2, HEIGHT*0.4, rMax*0.5, 4); 24 | target(WIDTH*0.6, HEIGHT*0.5, rMax, 10); 25 | target(WIDTH*0.9, HEIGHT*0.3, rMax*0.25, 6); 26 | 27 | // save the image 28 | snapshot("targets.png") -------------------------------------------------------------------------------- /_examples/targets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasepe/g2d/802d7884f029f95f984f7962429ebcee64d1bc01/_examples/targets.png -------------------------------------------------------------------------------- /_extras/gtksourceview-3.0/language-specs/g2d.lang: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | text/x-g2d 10 | *.g2d 11 | // 12 | 13 | 14 | 15 |