Skip to content

Commit

Permalink
Fix bug with rendering axes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Mar 12, 2024
1 parent 4a67b56 commit 9f30f1d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
(modules line)
(libraries joy))

(executable
(name parallel_lines)
(modules parallel_lines)
(libraries joy))

(executable
(name higher_transforms)
(modules higher_transforms)
Expand Down
24 changes: 6 additions & 18 deletions examples/line.ml
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
open Joy

let size = 800
let interval = 16
let line_interval = 800 / interval
let rec range a b = if a > b then [] else a :: range (a + 1) b
let inc x = x + 1

let _ =
init ~size:(size, size) ();
let half_size = size / 2 in
let lines =
List.map
(fun i ->
let newx = i |> inc |> ( * ) line_interval in
line
~a:(point (newx - half_size) (-half_size))
(point (newx - half_size) half_size))
(range 0 interval)
in
show lines;
init ();
let l1 = line (point 50 50) in
let l2 = line (point (-50) 50) in
let l3 = line ~a:(point (-50) 50) (point 50 50) in
show [l1; l2; l3];
write ~filename:"line.png" ()

22 changes: 22 additions & 0 deletions examples/parallel_lines.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
open Joy

let size = 800
let interval = 16
let line_interval = 800 / interval
let rec range a b = if a > b then [] else a :: range (a + 1) b
let inc x = x + 1

let _ =
init ~size:(size, size) ();
let half_size = size / 2 in
let lines =
List.map
(fun i ->
let newx = i |> inc |> ( * ) line_interval in
line
~a:(point (newx - half_size) (-half_size))
(point (newx - half_size) half_size))
(range 0 interval)
in
show lines;
write ~filename:"parallel_lines.png" ()
2 changes: 2 additions & 0 deletions lib/color.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let transparent = (0, 0, 0, 0.0)
For use w/ `Context.background` *)
let rgb r g b = (r, g, b, 1.0)

let color ?(a = 1.0) r g b = (r, g, b, a)

(** RGB code for black *)
let black = rgb 0 0 0

Expand Down
1 change: 1 addition & 0 deletions lib/color.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type color = int * int * int * float

val rgb : int -> int -> int -> color
val color : ?a:float -> int -> int -> int -> color

val black : color
val white : color
Expand Down
12 changes: 4 additions & 8 deletions lib/joy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ type context = Context.context
let show = Context.show
let set_line_width = Context.set_line_width

(* let init ?(background = Color.white) ?(line_width = 2) ?(size = (500, 500))
?(axes = false) () =
Context.init_context (Color.opaque background) (float_of_int line_width) size
axes *)

let init ?(size = (500, 500)) ?(line_width = 2) ?(axes = false) _ =
let ctx = Backend_cairo.create ~background_color:Color.white ~size ~line_width ~axes in
let ctx_container = Context.CairoContext ctx in
Context.set_default ctx_container;
if axes then
let half_w, half_h = ctx.size |> Util.tmap float_of_int |> Util.tmap ((/.) 2.) in
let x_axis = line ~a:{x = -.half_w; y = 0.} {x = half_w; y = 0.} in
let y_axis = line ~a:{x = 0.; y = -.half_h} {x = 0.; y = half_h} in
let half_w, half_h = ctx.size |> Util.tmap float_of_int |> Util.tmap (fun x -> x /. 2.0) in
let gray = Color.color 128 128 128 ~a:0.5 in
let x_axis = line ~a:{x = -.half_w; y = 0.} {x = half_w; y = 0.} |> with_stroke gray in
let y_axis = line ~a:{x = 0.; y = -.half_h} {x = 0.; y = half_h} |> with_stroke gray in
show ~ctx:ctx_container [x_axis; y_axis]

let write ?(filename = "joy.png") () =
Expand Down

0 comments on commit 9f30f1d

Please sign in to comment.