Touch up some documentation.
This commit is contained in:
parent
917d0f34fe
commit
85413cadc6
3 changed files with 34 additions and 14 deletions
|
@ -11,6 +11,11 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Initialise arbitrary bounds.
|
||||
|
||||
* `x0` - The x coordinate of the bottom-left pixel.
|
||||
* `y0` - The y coordinate of the bottom-left pixel.
|
||||
* `x1` - The x coordinate of the top-right pixel.
|
||||
* `y1` - The y coordinate of the top-right pixel.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Bounds.init(0, 0, 5, 5)
|
||||
|
@ -22,6 +27,8 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Return the bounding box required to encapsulate the shape.
|
||||
|
||||
* `shape` - A shape whose bounds you want to measure.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -29,7 +36,7 @@ defmodule Vivid.Bounds do
|
|||
#Vivid.Bounds<[min: #Vivid.Point<{0.0, 0.0}>, max: #Vivid.Point<{20.0, 20.0}>]>
|
||||
"""
|
||||
@spec bounds(Shape.t) :: Bounds.t
|
||||
def bounds(%Bounds{} = bounds), do: bounds
|
||||
def bounds(%Bounds{} = shape), do: shape
|
||||
def bounds(shape) do
|
||||
{min, max} = Of.bounds(shape)
|
||||
%Bounds{min: min, max: max}
|
||||
|
@ -38,6 +45,8 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Returns the width of a shape.
|
||||
|
||||
* `shape` - The shape whose width you want to measure.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -45,12 +54,14 @@ defmodule Vivid.Bounds do
|
|||
20.0
|
||||
"""
|
||||
@spec width(Shape.t) :: number
|
||||
def width(%Bounds{min: %Point{x: x0}, max: %Point{x: x1}}), do: abs(x1 - x0)
|
||||
def width(%Bounds{min: %Point{x: x0}, max: %Point{x: x1}} = _shape), do: abs(x1 - x0)
|
||||
def width(shape), do: shape |> bounds |> width
|
||||
|
||||
@doc """
|
||||
Returns the height of a shape.
|
||||
|
||||
* `shape` - The shape whose height you want to measure.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -64,6 +75,8 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Returns the bottom-left point of the bounds.
|
||||
|
||||
* `shape` - The shape whose bottom-left pixel you want to find.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -71,12 +84,14 @@ defmodule Vivid.Bounds do
|
|||
#Vivid.Point<{0.0, 0.0}>
|
||||
"""
|
||||
@spec min(Shape.t) :: Point.t
|
||||
def min(%Bounds{min: min}), do: min
|
||||
def min(%Bounds{min: min} = _shape), do: min
|
||||
def min(shape), do: shape |> bounds |> min
|
||||
|
||||
@doc """
|
||||
Returns the top-right point of the bounds.
|
||||
|
||||
* `shape` - The shape whose top-right pixel you want to find.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -90,6 +105,8 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Returns the center point of the bounds.
|
||||
|
||||
* `shape` - The shape whose center-most pixel you want to find.
|
||||
|
||||
## Example
|
||||
|
||||
iex> Vivid.Circle.init(Vivid.Point.init(10,10), 10)
|
||||
|
@ -108,6 +125,9 @@ defmodule Vivid.Bounds do
|
|||
@doc """
|
||||
Returns true if the point is within the bounds.
|
||||
|
||||
* `shape` - A shape you wish to test.
|
||||
* `point` - The point you wish to test.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Vivid.Bounds.init(0, 0, 10, 10)
|
||||
|
@ -135,6 +155,6 @@ defmodule Vivid.Bounds do
|
|||
false
|
||||
"""
|
||||
@spec contains?(Shape.t, Point.t) :: boolean
|
||||
def contains?(%Bounds{min: %Point{x: x0, y: y0}, max: %Point{x: x1, y: y1}}, %Point{x: x, y: y}) when x0 <= x and x <= x1 and y0 <= y and y <= y1, do: true
|
||||
def contains?(_, _), do: false
|
||||
def contains?(%Bounds{min: %Point{x: x0, y: y0}, max: %Point{x: x1, y: y1}} = _shape, %Point{x: x, y: y} = _point) when x0 <= x and x <= x1 and y0 <= y and y <= y1, do: true
|
||||
def contains?(_shape, _point), do: false
|
||||
end
|
||||
|
|
|
@ -59,7 +59,7 @@ defmodule Vivid.Point do
|
|||
def swap_xy(%Point{x: x, y: y}), do: Point.init(y, x)
|
||||
|
||||
@doc """
|
||||
Return the vector in `x` and `y` between point `a` and point `b`.
|
||||
Return the vector in x and y between point `a` and point `b`.
|
||||
|
||||
## Example
|
||||
|
||||
|
@ -70,7 +70,7 @@ defmodule Vivid.Point do
|
|||
{10, 10}
|
||||
"""
|
||||
@spec vector(Point.t, Point.t) :: {number, number}
|
||||
def vector(%Point{x: x0, y: y0}, %Point{x: x1, y: y1}) do
|
||||
def vector(%Point{x: x0, y: y0} = _a, %Point{x: x1, y: y1} = _b) do
|
||||
{x1 - x0, y1 - y0}
|
||||
end
|
||||
|
||||
|
|
|
@ -14,19 +14,19 @@ defmodule Vivid.Transform.Point do
|
|||
@type radians :: number
|
||||
|
||||
@doc """
|
||||
Translate a point (ie move it) by adding `x` and `y` to it's coordinates.
|
||||
Translate `point` (ie move it) by adding `x` and `y` to it's coordinates.
|
||||
"""
|
||||
@spec translate(Point.t, number, number) :: Point.t
|
||||
def translate(%Point{x: x0, y: y0}, x, y), do: Point.init(x0 + x, y0 + y)
|
||||
def translate(%Point{x: x0, y: y0} = _point, x, y), do: Point.init(x0 + x, y0 + y)
|
||||
|
||||
@doc """
|
||||
Scale a point (ie move it) by multiplying it's distance from the `0`, `0` point by `x_factor` and `y_factor`.
|
||||
Scale `point` (ie move it) by multiplying it's distance from the `0`, `0` point by `x_factor` and `y_factor`.
|
||||
"""
|
||||
@spec scale(Point, number, number) :: Point.t
|
||||
def scale(%Point{} = point, x_factor, y_factor), do: scale(point, x_factor, y_factor, Point.init(0,0))
|
||||
|
||||
@doc """
|
||||
Scale a point (ie move it) by multiplying it's distance from the origin point by `x_factor` and `y_factor`.
|
||||
Scale `point` (ie move it) by multiplying it's distance from the origin point by `x_factor` and `y_factor`.
|
||||
"""
|
||||
@spec scale(Point, number, number, Point.t) :: Point.t
|
||||
def scale(%Point{x: x, y: y} = _point, x_factor, y_factor, %Point{x: xo, y: yo} = _origin) do
|
||||
|
@ -36,16 +36,16 @@ defmodule Vivid.Transform.Point do
|
|||
end
|
||||
|
||||
@doc """
|
||||
Rotate a point `degrees` around an origin point.
|
||||
Rotate `point` `degrees` around an `origin` point.
|
||||
"""
|
||||
@spec rotate(Point.t, Point.t, degrees) :: Point.t
|
||||
def rotate(point, origin, degrees), do: rotate_radians(point, origin, degrees_to_radians(degrees))
|
||||
|
||||
@doc """
|
||||
Rotate a point `radians` around an origin point.
|
||||
Rotate `point` `radians` around an `origin` point.
|
||||
"""
|
||||
@spec rotate_radians(Point.t, Point.t, radians) :: Point.t
|
||||
def rotate_radians(%Point{x: x0, y: y0} = _point, %Point{x: x1, y: y1} = _center, radians) do
|
||||
def rotate_radians(%Point{x: x0, y: y0} = _point, %Point{x: x1, y: y1} = _origin, radians) do
|
||||
x = cos(radians) * (x0 - x1) - sin(radians) * (y0 - y1) + x1
|
||||
y = sin(radians) * (x0 - x1) + cos(radians) * (y0 - y1) + y1
|
||||
Point.init(x,y)
|
||||
|
|
Loading…
Reference in a new issue