Mercurial > projects > doodle
changeset 1:c18e3f93d114
Implemented Rectangle2
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Wed, 13 May 2009 17:50:25 +0930 |
parents | e907d2c54ec3 |
children | d6f44347373d |
files | gui.d tk/geometry2.d |
diffstat | 2 files changed, 120 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/gui.d Wed May 13 15:42:39 2009 +0930 +++ b/gui.d Wed May 13 17:50:25 2009 +0930 @@ -10,7 +10,11 @@ import tk.types; import tk.events; +import std.stdio; +import tk.geometry2; + void main(string[] args) { + /* Main.init(args); auto window = new MainWindow("Title"); auto event_handler = new ToolStack(); @@ -18,4 +22,16 @@ window.add(canvas); window.showAll(); Main.run(); + */ + + Point2 p1 = Point2(3.0, 5.0); + writefln("%s", p1); + + Point2 p2 = Point2(1.0, 2.0); + writefln("%s", p2); + + writefln("%s", p1 - p2); + + Rectangle2 r = Rectangle2(p1, p2); + writefln("%s", r); }
--- a/tk/geometry2.d Wed May 13 15:42:39 2009 +0930 +++ b/tk/geometry2.d Wed May 13 17:50:25 2009 +0930 @@ -1,12 +1,114 @@ module tk.geometry2; +private import std.stdio; +private import std.math; + struct Point2 { - this(double x, double y) { + 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 { - immutable double _x, _y; + 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; + } +}