Skip to content

Commit

Permalink
Merge pull request #11 from ararslan/aa/nanmath
Browse files Browse the repository at this point in the history
Add dependency on NaNMath for IEEE min/max
  • Loading branch information
timholy authored Mar 25, 2017
2 parents 615dbd4 + b9e593d commit 2993c7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
julia 0.4
Colors
Compat 0.17.0
NaNMath 0.2.4
42 changes: 17 additions & 25 deletions src/Graphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Graphics
import Base: +, -, *, /, &, fill, norm
using Colors
using Compat
using NaNMath

if isdefined(Base, :scale)
import Base: scale
Expand Down Expand Up @@ -56,15 +57,6 @@ export
# TODO: rendering pipeline API


# Utilities

# IEEE754 compliant min/max (prefers value over quiet NaN)
nanmin(x, y) = ifelse((y < x) | (signbit(y) > signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))
nanmax(x, y) = ifelse((y > x) | (signbit(y) < signbit(x)),
ifelse(isnan(y), x, y), ifelse(isnan(x), y, x))


# Part 1. geometric primitives

immutable Vec2
Expand Down Expand Up @@ -103,21 +95,21 @@ BoundingBox() = BoundingBox(NaN, NaN, NaN, NaN)
function BoundingBox(p0::Point, points::Point...)
xmin, xmax, ymin, ymax = p0.x, p0.x, p0.y, p0.y
for p in points
xmin = nanmin(xmin, p.x)
xmax = nanmax(xmax, p.x)
ymin = nanmin(ymin, p.y)
ymax = nanmax(ymax, p.y)
xmin = NaNMath.min(xmin, p.x)
xmax = NaNMath.max(xmax, p.x)
ymin = NaNMath.min(ymin, p.y)
ymax = NaNMath.max(ymax, p.y)
end
return BoundingBox(xmin, xmax, ymin, ymax)
end

function BoundingBox(bb0::BoundingBox, bboxes::BoundingBox...)
xmin, xmax, ymin, ymax = bb0.xmin, bb0.xmax, bb0.ymin, bb0.ymax
for bb in bboxes
xmin = nanmin(xmin, bb.xmin)
xmax = nanmax(xmax, bb.xmax)
ymin = nanmin(ymin, bb.ymin)
ymax = nanmax(ymax, bb.ymax)
xmin = NaNMath.min(xmin, bb.xmin)
xmax = NaNMath.max(xmax, bb.xmax)
ymin = NaNMath.min(ymin, bb.ymin)
ymax = NaNMath.max(ymax, bb.ymax)
end
return BoundingBox(xmin, xmax, ymin, ymax)
end
Expand All @@ -137,17 +129,17 @@ xrange(x) = xmin(x), xmax(x)
yrange(x) = ymin(x), ymax(x)

function (+)(bb1::BoundingBox, bb2::BoundingBox)
BoundingBox(nanmin(bb1.xmin, bb2.xmin),
nanmax(bb1.xmax, bb2.xmax),
nanmin(bb1.ymin, bb2.ymin),
nanmax(bb1.ymax, bb2.ymax))
BoundingBox(NaNMath.min(bb1.xmin, bb2.xmin),
NaNMath.max(bb1.xmax, bb2.xmax),
NaNMath.min(bb1.ymin, bb2.ymin),
NaNMath.max(bb1.ymax, bb2.ymax))
end

function (&)(bb1::BoundingBox, bb2::BoundingBox)
BoundingBox(nanmax(bb1.xmin, bb2.xmin),
nanmin(bb1.xmax, bb2.xmax),
nanmax(bb1.ymin, bb2.ymin),
nanmin(bb1.ymax, bb2.ymax))
BoundingBox(NaNMath.max(bb1.xmin, bb2.xmin),
NaNMath.min(bb1.xmax, bb2.xmax),
NaNMath.max(bb1.ymin, bb2.ymin),
NaNMath.min(bb1.ymax, bb2.ymax))
end

function deform(bb::BoundingBox, dl, dr, dt, db)
Expand Down

0 comments on commit 2993c7b

Please sign in to comment.