Skip to content

Commit

Permalink
Merge pull request #2 from JanCVanB/add_examples
Browse files Browse the repository at this point in the history
Add more examples
  • Loading branch information
JanCVanB authored Feb 20, 2022
2 parents 4a12599 + 4ee6adb commit a511d5b
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 261 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[Plotters](https://github.com/38/plotters)
= <3

![hello world example image](./examples/hello_world.svg)
![example image](./examples/math.svg)

## How to example

Expand Down
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# built executables
hello_world
math
shapes
Binary file removed examples/hello_world.jpg
Binary file not shown.
Binary file modified examples/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 18 additions & 28 deletions examples/hello_world.roc
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
app "hello_world"
packages { pf: "../platform" }
imports [ pf.Config.{blue, green, red, cyan} ]
imports [ pf.Config ]
provides [ config ] to pf

config =
{
outputFilePath: "./examples/hello_world.svg",
outputFilePath: "./examples/hello_world.png",
title: "Hello, World!",
subtitle: "",
width: 1024,
height: 768,
width: 800,
height: 600,
lines: [
{ name: "cosine", color: green, points: cos },
{ name: "cosine x 2", color: cyan, points: cosX2 },
{ name: "sine", color: blue, points: sin },
{ name: "sine x 2", color: red, points: sinX2 },
{ name: "up", color: Config.green, points: [P2 -1 -1, P2 1 1] },
{ name: "down", color: Config.red, points: [P2 -1 1, P2 1 -1] },
],
bounds: {
xMin: -3.2,
xMax: 3.2,
yMin: -2.1,
yMax: 2.1,
},
fonts: {
titleFamily: "sans-serif",
titleSize: 60,
subtitleFamily: "sans-serif",
subtitleSize: 40,
xMin: -1.1,
xMax: 1.1,
yMin: -1.1,
yMax: 1.1,
},
labels: {
xCount: 20,
yCount: 10,
xCount: 3,
yCount: 3,
},
layout: {
chartMargin: 5,
labelArea: 50,
},
fonts: {
titleFamily: "sans-serif",
titleSize: 60,
subtitleFamily: "sans-serif",
subtitleSize: 40,
},
}

pi = 3.141592653589793
ok = \r -> Result.withDefault r 0
domain = List.range -100 101 |> List.map (\i -> pi * (Num.toFloat i) / 100 |> ok)
cos = domain |> List.map (\x -> P2 x (Num.cos x))
sin = domain |> List.map (\x -> P2 x (Num.sin x))
cosX2 = domain |> List.map (\x -> P2 x (2 * Num.cos x))
sinX2 = domain |> List.map (\x -> P2 x (2 * Num.sin x))
207 changes: 0 additions & 207 deletions examples/hello_world.svg

This file was deleted.

47 changes: 47 additions & 0 deletions examples/math.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
app "math"
packages { pf: "../platform" }
imports [ pf.Config ]
provides [ config ] to pf

config =
{
outputFilePath: "./examples/math.svg",
title: "Math",
subtitle: "This is what math looks like.",
width: 800,
height: 600,
lines: [
{ name: "cosine", color: Config.green, points: cos },
{ name: "cosine x 2", color: Config.cyan, points: cosX2 },
{ name: "sine", color: Config.blue, points: sin },
{ name: "- sine", color: Config.red, points: sinNeg },
],
bounds: {
xMin: -3.2,
xMax: 3.2,
yMin: -2.1,
yMax: 2.1,
},
labels: {
xCount: 20,
yCount: 10,
},
layout: {
chartMargin: 5,
labelArea: 50,
},
fonts: {
titleFamily: "sans-serif",
titleSize: 60,
subtitleFamily: "sans-serif",
subtitleSize: 40,
},
}

pi = 3.141592653589793
ok = \r -> Result.withDefault r 0
domain = List.range -100 101 |> List.map (\i -> pi * (Num.toFloat i) / 100 |> ok)
cos = domain |> List.map (\x -> P2 x (Num.cos x))
sin = domain |> List.map (\x -> P2 x (Num.sin x))
cosX2 = domain |> List.map (\x -> P2 x (2 * Num.cos x))
sinNeg = domain |> List.map (\x -> P2 x (0 - Num.sin x))
210 changes: 210 additions & 0 deletions examples/math.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/shapes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions examples/shapes.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
app "shapes"
packages { pf: "../platform" }
imports [ pf.Config ]
provides [ config ] to pf

config =
{
outputFilePath: "./examples/shapes.jpg",
title: "Shapes",
subtitle: "",
width: 800,
height: 600,
lines: [
{
name: "rectangle",
color: Config.red,
points: [
P2 0.1 0.3,
P2 0.2 0.3,
P2 0.2 0.2,
P2 0.1 0.2,
P2 0.1 0.3,
],
},
{
name: "triangle",
color: Config.green,
points: [
P2 0.4 0.6,
P2 0.5 1.0,
P2 0.6 0.6,
P2 0.4 0.6,
],
},
{
name: "plus",
color: Config.blue,
points: [
P2 0.6 0.3, P2 0.8 0.3,
P2 0.7 0.3,
P2 0.7 0.4, P2 0.7 0.2,
],
},
],
bounds: {
xMin: 0,
xMax: 1,
yMin: 0,
yMax: 1,
},
labels: {
xCount: 5,
yCount: 5,
},
layout: {
chartMargin: 5,
labelArea: 50,
},
fonts: {
titleFamily: "sans-serif",
titleSize: 60,
subtitleFamily: "sans-serif",
subtitleSize: 40,
},
}
46 changes: 23 additions & 23 deletions platform/Config.roc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface Config
exposes [ Config, black, white, red, green, blue, cyan, magenta, yellow ]
exposes [ Config, black, blue, cyan, green, magenta, red, white, yellow ]
imports []

Bounds :
Expand All @@ -18,14 +18,20 @@ Color :
b: U8,
}

black = { r: 0, g: 0, b: 0 }
white = { r: 255, g: 255, b: 255 }
red = { r: 255, g: 0, b: 0 }
green = { r: 0, g: 255, b: 0 }
blue = { r: 0, g: 0, b: 255 }
cyan = { r: 0, g: 255, b: 255 }
magenta = { r: 255, g: 0, b: 255 }
yellow = { r: 255, g: 255, b: 0 }
# TODO: Make this a `List` and support it in the platform (with `Vec`?)
Config :
{
outputFilePath : Str,
title : Str,
subtitle : Str,
width : U32,
height : U32,
lines : List Line,
bounds : Bounds,
fonts : Fonts,
labels : Labels,
layout : Layout,
}

Fonts :
{
Expand Down Expand Up @@ -54,17 +60,11 @@ Line :
points: List [P2 F64 F64],
}

# TODO: Make this a `List` and support it in the platform (with `Vec`?)
Config :
{
outputFilePath : Str,
title : Str,
subtitle : Str,
width : U32,
height : U32,
lines : List Line,
bounds : Bounds,
fonts : Fonts,
labels : Labels,
layout : Layout,
}
black = { r: 0, g: 0, b: 0 }
blue = { r: 0, g: 0, b: 255 }
cyan = { r: 0, g: 255, b: 255 }
green = { r: 0, g: 255, b: 0 }
magenta = { r: 255, g: 0, b: 255 }
red = { r: 255, g: 0, b: 0 }
white = { r: 255, g: 255, b: 255 }
yellow = { r: 255, g: 255, b: 0 }
4 changes: 2 additions & 2 deletions platform/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ pub fn roc_config() -> Config {
fn call(_: &mut Config);
}
let mut config = Config::default();
unsafe { call(&mut config) };
dbg!(config)
unsafe { call(&mut config) }
config
}

0 comments on commit a511d5b

Please sign in to comment.