Skip to content

Commit c806e8f

Browse files
committed
document shape
1 parent eb6eea8 commit c806e8f

File tree

4 files changed

+147
-10
lines changed

4 files changed

+147
-10
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
_build
1010

1111
# Generated
12-
docs.public
13-
docs.private
12+
doc.public
13+
doc.private
1414

1515
# Temporary files
1616
*.swp

core/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TEST=test.byte
88
LIB_BYTE=p5.cma
99
LIB_OPT=p5.cmxa
1010
OCAMLBUILD=ocamlbuild -use-ocamlfind
11-
PKGS=oUnit,graphics
11+
PKGS=oUnit,graphics,threads,cairo2,lablgtk2,cairo2-gtk
1212

1313
default: lib install
1414

@@ -35,12 +35,12 @@ docs: docs-public docs-private
3535

3636
docs-public: build
3737
mkdir -p doc.public
38-
ocamlfind ocamldoc -I _build -package $(PKGS) \
38+
ocamlfind ocamldoc -I _build -thread -package $(PKGS) \
3939
-html -stars -d doc.public $(MLIS)
4040

4141
docs-private: build
4242
mkdir -p doc.private
43-
ocamlfind ocamldoc -I _build -package $(PKGS) \
43+
ocamlfind ocamldoc -I _build -thread -package $(PKGS) \
4444
-html -stars -d doc.private \
4545
-inv-merge-ml-mli -m A -hide-warnings $(MLS)
4646

core/shape.ml

+18-4
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,36 @@ let empty = Empty
108108
let name name shape =
109109
Name (shape, name)
110110

111-
let rec find_named shape name =
111+
let rec find_named name shape =
112112
match shape with
113113
| Shape _ -> None
114114
| Group shapes ->
115115
begin
116-
match List.map (fun shape -> find_named shape name) shapes with
116+
match List.map (find_named name) shapes with
117117
| hd :: _ -> hd
118118
| _ -> None
119119
end
120-
| Paint (nest_shape, _) -> find_named nest_shape name
120+
| Paint (nest_shape, _) -> find_named name nest_shape
121121
| Name (nest_shape, nest_name) ->
122122
if name = nest_name then Some nest_shape
123-
else find_named nest_shape name
123+
else find_named name nest_shape
124124
| Background _ -> None
125125
| Empty -> None
126126

127+
let rec find_all_named name shape =
128+
match shape with
129+
| Shape _ -> []
130+
| Group shapes ->
131+
begin
132+
List.fold_left (@) [] (List.map (find_all_named name) shapes)
133+
end
134+
| Paint (nest_shape, _) -> find_all_named name nest_shape
135+
| Name (nest_shape, nest_name) ->
136+
if name = nest_name then [nest_shape]
137+
else find_all_named name nest_shape
138+
| Background _ -> []
139+
| Empty -> []
140+
127141
let transform_vertex angle (scale_x, scale_y, scale_mag)
128142
(func : vector -> vector) (vertex : vertex) : vertex =
129143
match vertex with

core/shape.mli

+124-1
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,179 @@
11

2+
(** [Shape] contains the [Shape.t] type and utilities for creating and
3+
manipulating shapes. A [Shape.t] is a vector representation of a
4+
drawing command and serves as the basis for all drawing operations
5+
in p5ml. *)
6+
27
open Color
38
open Paint
49
open Math
510
open Bezier
611

12+
(** {2 Shapes} *)
13+
714
type vector = Vector.t
815

16+
(** The type of a vertex, a single piece of vector information that
17+
forms a shape. *)
918
type vertex = private
19+
(** [MoveTo vec] picks up the pen and places it down at [vec]. *)
1020
| MoveTo of vector
21+
22+
(** [LineTo vec] draws from the pen location to [vec]. *)
1123
| LineTo of vector
24+
25+
(** [BezierTo v2 v3 v4] draws from the pen location, using the pen location
26+
as the beginning anchor point, [v2] and [v3] as the anchor points, and
27+
[v4] as the ending anchor point. *)
1228
| BezierTo of vector * vector * vector
29+
30+
(** [Arc center size phi theta1 theta2] draws an arc centered at [center]
31+
and inscribed in [rect point ~align:`Center size]. [phi] is the offset
32+
angle of the rectangle, and [theta1] and [theta2] are the start and end
33+
angles of the arc, respectively. The pen is not moved to the start; it
34+
continues from where it left off and connects to where the arc begins. *)
1335
| Arc of vector * vector * float * float * float
36+
37+
(** [ClosePath] closes the path by drawing a line from the current pen
38+
position to the last pen down position (created by [MoveTo]). *)
1439
| ClosePath
1540

41+
(** The type of a shape, a component that can be displayed.
42+
The [Shape] constructor contains vertices. *)
1643
type t = private
44+
(** [Shape vertices] is a complex shape defined by [vertices]. Well-formed
45+
shapes must begin with [MoveTo] and cannot have a [ClosePath] anywhere
46+
except (optionally) the end. *)
1747
| Shape of vertex list
48+
49+
(** [Group shapes] is the compound shape that draws each shape in [shapes]
50+
in order, i.e. with the last shape on top. *)
1851
| Group of t list
52+
53+
(** [Paint shape update] is the shape that draws [shape] with the paint
54+
modification specified by [update]. Paint updates are specified one
55+
attribute at a time (e.g. just the stroke) so that all other attributes
56+
are inherited from the parent shape. *)
1957
| Paint of t * paint_update
58+
59+
(** [Name shape n] is [shape] tagged with name [n]. This shape is functionally
60+
equivalent to [shape], but the name may be used for identifying small
61+
components of larger shapes. *)
2062
| Name of t * string
63+
64+
(** [Background color] is the shape that fills the background with [color],
65+
erasing what was already present. This separate constructor is necessary
66+
because a [rect] would be affected by transformations. *)
2167
| Background of color
68+
69+
(** [Empty] is the shape that draws nothing. *)
2270
| Empty
2371

72+
(** {2 Constructing Basic Shapes} *)
73+
74+
(** [poly vertices] is the shape representing the polygon with [vertices]. *)
2475
val poly : ?close : [`Close | `Open] -> vector list -> t
76+
77+
(** [point vertex] is the shape representing the point at position [vertex]. *)
2578
val point : vector -> t
79+
80+
(** [line v1 v2] is the shape representing the line between [v1] and [v2]. *)
2681
val line : vector -> vector -> t
82+
83+
(** [rect point size] is the shape representing the rectangle with corner at
84+
[point] and dimensions [size]. *)
2785
val rect : vector -> ?align : [`Corner | `Center] -> vector -> t
86+
87+
(** [quad v1 v2 v3 v3] is [polygon [v1; v2; v3; v4]]. *)
2888
val quad : vector -> vector -> vector -> vector -> t
89+
90+
(** [triangle v1 v2 v3] is [polygon [v1; v2; v3]]. *)
2991
val triangle : vector -> vector -> vector -> t
92+
93+
(** [ellipse point size] is the shape representing the ellipse centered at
94+
[point] and inscribed in [rect point ~align:`Center size]. *)
3095
val ellipse : vector -> ?align : [`Corner | `Center] -> vector -> t
96+
97+
(** [circle point radius] is [ellipse point (radius, radius)]. *)
3198
val circle : vector -> ?align : [`Corner | `Center] -> float -> t
99+
100+
(** [arc point size theta1 theta2] is the shape representing the arc (portion
101+
of a full ellipse) centered at [point] and inscribed in
102+
[rect point ~align:`Center size] from angle [theta1] to [theta2]. *)
32103
val arc : vector -> ?align : [`Corner | `Center] -> vector ->
33104
?stroke_mode : [`Closed | `Open] ->
34105
?fill_mode : [`Pie | `Chord] -> ?phi : float -> float -> float -> t
106+
107+
(** [bezier bez] is the shape representing the Bezier curve [bez]. *)
35108
val bezier : Bezier.t -> t
36109

110+
(** {2 Constructing Meta-shapes} *)
111+
112+
(** [group shapes] is the shape that draws [shapes] in order. *)
37113
val group : t list -> t
114+
115+
(** [shape sh tr] is the shape [sh] translated by [tr]. *)
38116
val shape : t -> vector -> t
117+
118+
(** [background color] is the shape that sets the background to [color]. *)
39119
val background : color -> t
120+
121+
(** [empty] is the shape that draws nothing. *)
40122
val empty : t
41123

124+
(** {2 Applying Shape Paints} *)
125+
126+
(** [fill color shape] is [shape] with fill [color], used to draw the inside
127+
of shapes. *)
42128
val fill : color -> t -> t
129+
130+
(** [no_fill shape] is [shape] with fill cleared. *)
43131
val no_fill : t -> t
132+
133+
(** [stroke color shape] is [shape] with stroke [color], used to draw the
134+
outline of shapes. *)
44135
val stroke : color -> t -> t
136+
137+
(** [no_stroke shape] is [shape] with stroke cleared. *)
45138
val no_stroke : t -> t
139+
140+
(** [stroke_weight w shape] is [shape] with stroke weight [w], the width of
141+
the stroke lines. *)
46142
val stroke_weight : float -> t -> t
143+
144+
(** [stroke_cap c shape] is [shape] with stroke cap [c], the way that the ends
145+
of lines are drawn. *)
47146
val stroke_cap : [`Round | `Square | `Project] -> t -> t
147+
148+
(** [stroke_join j shape] is [shape] with stroke join [j], the way that vertices
149+
between two lines are drawn. *)
48150
val stroke_join : [`Miter | `Bevel | `Round] -> t -> t
49151

152+
(** {2 Names} *)
153+
154+
(** [name n shape] is [shape] tagged with name [n], which can be looked up
155+
with [find_named]. *)
50156
val name : string -> t -> t
51157

52-
val find_named : t -> string -> t option
158+
(** [find_named name shape] is [Some found] where [found] is the first shape
159+
tagged with [name] in [shape], or [None] if [shape] contains no shapes
160+
tagged [name]. *)
161+
val find_named : string -> t -> t option
53162

163+
(** [find_all_named name shape] is the list of all shapes in [shape] that are
164+
tagged with [name]. *)
165+
val find_all_named : string -> t -> t list
166+
167+
(** {2 Transformations} *)
168+
169+
(** [translate tr shape] is [shape] translated by offset [tr]. *)
54170
val translate : vector -> t -> t
171+
172+
(** [scale (scale_x, scale_y) shape] is [shape] scaled by factors of [scale_x]
173+
and [scale_y] in the x and y directions, respectively, about the origin.
174+
To achieve scaling about a different center, first use [translate]. *)
55175
val scale : vector -> t -> t
176+
177+
(** [rotate angle shape] is [shape] rotated [angle] radians about the origin.
178+
To achieve rotation about a different center, first use [translate]. *)
56179
val rotate : float -> t -> t

0 commit comments

Comments
 (0)