From 9f30f1de0d5dea707cd8399beb1b22ffec081f48 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Tue, 12 Mar 2024 11:27:29 +0530 Subject: [PATCH] Fix bug with rendering axes --- examples/dune | 5 +++++ examples/line.ml | 24 ++++++------------------ examples/parallel_lines.ml | 22 ++++++++++++++++++++++ lib/color.ml | 2 ++ lib/color.mli | 1 + lib/joy.ml | 12 ++++-------- 6 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 examples/parallel_lines.ml diff --git a/examples/dune b/examples/dune index 47a75b9..a88c9b8 100644 --- a/examples/dune +++ b/examples/dune @@ -53,6 +53,11 @@ (modules line) (libraries joy)) +(executable + (name parallel_lines) + (modules parallel_lines) + (libraries joy)) + (executable (name higher_transforms) (modules higher_transforms) diff --git a/examples/line.ml b/examples/line.ml index 2c250c1..d735b8c 100644 --- a/examples/line.ml +++ b/examples/line.ml @@ -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" () + diff --git a/examples/parallel_lines.ml b/examples/parallel_lines.ml new file mode 100644 index 0000000..a557726 --- /dev/null +++ b/examples/parallel_lines.ml @@ -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" () diff --git a/lib/color.ml b/lib/color.ml index 0b5838b..c7121d3 100644 --- a/lib/color.ml +++ b/lib/color.ml @@ -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 diff --git a/lib/color.mli b/lib/color.mli index 0e0496b..3a252dd 100644 --- a/lib/color.mli +++ b/lib/color.mli @@ -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 diff --git a/lib/joy.ml b/lib/joy.ml index 0798b5f..9019a0a 100644 --- a/lib/joy.ml +++ b/lib/joy.ml @@ -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") () =