Mercurial > projects > doodle
view tk/geometry.d @ 0:e907d2c54ec3
Initial import
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Wed, 13 May 2009 15:42:39 +0930 |
parents | |
children |
line wrap: on
line source
module tk.geometry; private import std.math; private import tk.misc; final class Point { this() { this(0.0, 0.0); } this(double x, double y) { _x = x; _y = y; } Point clone() const { return new Point(_x, _y); } override string toString() const { return std.string.format("(%f, %f)", _x, _y); } Point opAdd(in Vector v) const { return new Point(_x + v._x, _y + v._y); } Vector opSub(in Point p) const { return new Vector(_x - p._x, _y - p._y); } Point opSub(in Vector v) const { return new Point(_x - v._x, _y - v._y); } double x() const { return _x; } double y() const { return _y; } private immutable double _x, _y; } Point min_extents(in Point a, in Point b) { return new Point(min(a.x, b.x), min(a.y, b.y)); } Point max_extents(in Point a, in Point b) { return new Point(max(a.x, b.x), max(a.y, b.y)); } // final class Vector { this() { this(0.0, 0.0); } this(double x, double y) { _x = x; _y = y; } override string toString() const { return std.string.format("[%f, %f]", _x, _y); } Vector opAdd(in Vector v) const { return new Vector(_x + v._x, _y + v._y); } Vector opSub(in Vector v) const { return new Vector(_x - v._x, _y - v._y); } Vector opNeg() const { return new Vector(-_x, -_y); } Vector opMul_r(double d) const { return new Vector(d * _x, d * _y); } Vector opDiv(double d) const { return new Vector(_x / d, _y / d); } double length() const { return sqrt(_x * _x + _y * _y); } double x() const { return _x; } double y() const { return _y; } private immutable double _x, _y; } // final class Rectangle { this() { mPosition = new Point(); mSize = new Vector(); } this(Point position, Vector size) { this(position.x, position.y, size.x, size.y); } this(Point corner1, Point corner2) { this(corner1.x, corner1.y, corner2.x - corner1.x, corner2.y - corner1.y); } const(Point) position() const { return mPosition; } const(Vector) size() const { return mSize; } private { this(double x, double y, double w, double h) { if (w < 0.0) { x += w; w = -w; } if (h < 0.0) { y += h; h = -h; } mPosition = new Point(x, y); mSize = new Vector(w, h); } const(Point) mPosition; const(Vector) mSize; } }