Reference

std/geometry/circle

std/geometry/src/circle.trb

Circle, the disc in the plane: the shape a broad-phase test is cheapest against, because the test is one subtraction, one dot product and one comparison.

type Circle

type Circle<Scalar: Real = Float>

A disc: a midpoint and a radius. The boundary is inside it.

A circle is closed where a Rectangle is half-open, and the reason is that a rectangle tiles and a circle does not: nothing is lost by counting the boundary of a circle twice, and a great deal is lost by leaving it out of both of two touching circles.

Circle needs a Real scalar. A radius without a square root is a Rectangle's job.

Examples

const unit = Circle Vector2(0.0, 0.0), 1.0
print "{unit.contains(Vector2(1.0, 0.0))} {unit.contains(Vector2(1.1, 0.0))}"

Pitfalls

  • A negative radius describes no points, and Circle.isEmpty says so. Nothing normalizes it.

Related

field center

center: Vector2<Scalar>

The midpoint.

field radius

radius: Scalar

How far the disc reaches from its midpoint.

fn isEmpty

fn isEmpty(): Bool

Whether the radius is negative, in which case the disc describes no points.

fn contains

fn contains(point: Vector2<Scalar>): Bool

Whether the point is in the disc, boundary included.

fn encloses

fn encloses(other: Circle<Scalar>): Bool

Whether the other disc is inside this one, boundary included.

fn intersects

fn intersects(other: Circle<Scalar>): Bool

Whether the two discs share a point, touching included.

const first = Circle Vector2(0.0, 0.0), 1.0
print first.intersects(Circle(Vector2(2.0, 0.0), 1.0))

fn intersectsRectangle

fn intersectsRectangle(other: Rectangle<Scalar>): Bool

Whether the disc shares a point with the rectangle, which is a test against the nearest point of the rectangle.

fn closestPoint

fn closestPoint(to: Vector2<Scalar>): Vector2<Scalar>

The point of the disc that is nearest to the one given, which is the point itself where it is inside.

fn distanceTo

fn distanceTo(point: Vector2<Scalar>): Scalar

The distance from the point to the nearest point of the disc. Zero where it is inside.

fn translated

fn translated(by: Vector2<Scalar>): Circle<Scalar>

The same disc moved.

fn grown

fn grown(by: Scalar): Circle<Scalar>

The same disc with the radius grown by that much. A negative amount shrinks it.

fn bounds

fn bounds(): Rectangle<Scalar>

The smallest rectangle that holds the disc.

fn area

fn area(): Scalar

How much of the plane the disc covers: Real.pi of the scalar, times the radius squared.

fn circumference

fn circumference(): Scalar

How long the way around the disc is: Real.tau of the scalar, times the radius.