Mercurial > projects > doodle
view tk/geometry2.d @ 1:c18e3f93d114
Implemented Rectangle2
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Wed, 13 May 2009 17:50:25 +0930 |
parents | e907d2c54ec3 |
children | d6f44347373d |
line wrap: on
line source
module tk.geometry2; private import std.stdio; private import std.math; struct Point2 { this(in double x, in double y) { _x = x; _y = y; } Point2 opAdd(in Vector2 v) const { return Point2(_x + v._x, _y + v._y); } Point2 opSub(in Vector2 v) const { return Point2(_x - v._x, _y - v._y); } Vector2 opSub(in Point2 p) const { return Vector2(_x - p._x, _y - p._y); } string toString() /* const */ { return std.string.format("(%f, %f)", _x, _y); } double x() const { return _x; } double y() const { return _y; } private { double _x, _y; } } struct Vector2 { this(in double x, in double y) { _x = x; _y = y; } Vector2 opAdd(in Vector2 v) const { return Vector2(_x + v._x, _y + v._y); } Vector2 opSub(in Vector2 v) const { return Vector2(_x - v._x, _y - v._y); } Vector2 opNeg() const { return Vector2(-_x, -_y); } Vector2 opMul_r(double d) const { return Vector2(d * _x, d * _y); } Vector2 opDiv(double d) const { return Vector2(_x / d, _y / d); } double length() const { return sqrt(_x * _x + _y * _y); } string toString() /* const */ { return std.string.format("[%f, %f]", _x, _y); } double x() const { return _x; } double y() const { return _y; } private { double _x, _y; } } struct Rectangle2 { this(in Point2 position, Vector2 size) { this(position.x, position.y, size.x, size.y); } this(Point2 corner1, Point2 corner2) { this(corner1.x, corner1.y, corner2.x - corner1.x, corner2.y - corner1.y); } // Intersection Rectangle2 opAnd(in Rectangle2 r) const { // FIXME return Rectangle2(3.2, 5.3, 3.2, 2.3); } // Union Rectangle2 opOr(in Rectangle2 r) const { // FIXME return Rectangle2(3.2, 5.3, 5.8, 2.1); } string toString() { return std.string.format("{%s, %s}", mPosition, 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 = Point2(x, y); mSize = Vector2(w, h); } Point2 mPosition; Vector2 mSize; } }