JS-GIS 2 API reference

Geometrical objects

Table of contents


TPoint object

Description

TPoint represents a geometrical point, identified by the x and y coordinates.

Properties

x

Float: X coordinate of the point in the point's coordinate system.

y

Float: Y coordinate of the point in the point's coordinate system.

className

String: returns "TPoint".

Methods

assign(p)

Void: assigns to the object all the attributes of the given TPoint object p.

clone()

TPoint: returns a new copy of the object.

distance(p)

Float: returns the cartesian distance between the point and another point p.

doRound()

Void: rounds the object's coordinates to the nearest integer using the Math.round() method.

expand(leftX, upY, rightX, downY)

TRect: expands the point into a TRect object; the rect coordinates are calculated subtracting (leftX, upY) from min and summing (rightX, downY) to max.

If omitted, upY and rightX will be assumed being equal to leftX while downY will be assumed being equal to upY.

moveTo(newX, newY)

Void: moves the point to the new coordinates.

toAbsolute(origin)

TPoint: assuming the current instance has coordinates relative to the origin point origin, returns a new instance of the object having absolute coordinates.

toRelative(origin)

TPoint: returns a new instance of the object having coordinates expressed relatively to the origin point origin.

TPoint(aX, aY)

Class constructor: creates a new point object given the x and y coordinates.

Example:

p = new TPoint(150, 100)

translate(src, dest)

TPoint: returns a point representing the translation of the current object from coordinate system defined by the TRect object src into the coordinate system defined by the TRect object dest. The method assumes that the rectangle objects src and dest cover the same area.

within(r)

Boolean: returns true if the object is within the TRect object r.


TRect object

Description

TRect represents a geometrical rectangle.

Properties

className

String: returns "TRect".

height

Float: the rectangle's height.

max

TPoint: the upper right corner for geographical rects, and the lower right corner for graphical rects.

min

TPoint: the lower left corner for geographical rects, and the upper left corner for graphical rects.

width

Float: the rectangle's width.

xAxis, yAxis

Integer: these two variables return 1 or -1 according to the direction of the increasing values along the X axis and the Y axis respectively.
A value of 1 means from left to right on the X axis, and from top to bottom in the Y axis. This is a graphic coordinate system.
A value of -1 should be only used for yAxis to indicate a bottom-up order, suitable for geographic coordinate systems.

xAxis and yAxis are used only in the translate method of the TPoint object, and are not needed when dealing with objects in the same coordinate system. The values of 1 and -1 are merely conventional, and have no geometric meaning, apart from indicating a direction which is completely subjective.

Methods

assign(r)

Void: assigns to the object all the attributes of the given TRect object r.

centroid()

TPoint: returns the center of the rectangle.

clone()

TRect: returns a copy of the object.

doRound()

Void: rounds the object's coordinates to the nearest integer using the Math.round() method.

intersection(r)

TRect: returns the rectangle representing the intersection with the rect r.

Always test the result, like in the following example:
if (i = r1.intersection(r2)) {do domething with rect i}

intersects(obj)

Boolean: returns true if the rect intersects the geometrical object obj.

moveBy(deltaX, deltaY)

Void: moves a rectangle adding an offset to it's min and max point coordinates.

moveTo(newMinX, newMinY, newMaxX, newMaxY)

Void: moves or resizes the rectangle. If newMaxX and newMaxY are omitted, they are recalculated according to min and the former width and eight of the rectangle.

resize(newWidth, newHeight)

Void: resizes a rectangle. max is recalculated according to min and the new values of width and height.

toAbsolute(origin)

TRect: assuming the current instance has coordinates relative to the origin TPoint origin, returns a new instance of the object having absolute coordinates.

toRelative(origin)

TRect: returns a new instance of the object having coordinates expressed relatively to the origin TPoint origin.

translate(src, dest)

TRect: returns the rectangle representing the translation of the current object from coordinate system defined by the rectangle src into the coordinate system defined by the rectangle dest. The method assumes that the rectangle objects src and dest cover the same area.

TRect(aMinX, aMinY, aMaxX, aMaxY, bottomUpY, rightLeftX)

Class constructor: returns a new rectangle object given the min and max x and y couple of coordinates. The bottomUpY parameter defaults to false, thus indicating a graphical rectangle. rightLeftX should always left to its default value, false, to indicate a left-right order for the x axis, which fits both graphic and geographic reference systems.

within(r)

Boolean: returns true if the object is within the rectangle r.


Extending geometrical classes

TPoint and TRect are to be considered as geometric object primitives and, considering the poor nature of JavaScript as a drawing environment, should fit every need of drawing objects on a map.

A way of drawing more complex objects could be envisaged in drawing every point composing the object perimeter; this however, is left to future versions of the JS-GIS API.

Io order to make new objects work, however, the following methods should be implemented:

centroid() should return the centroid of the object as TPoint object. See the TRect_centroid implementation.
mbr() should return the minimum bounding rectangle as a TRect object.
transform(r1, r2) should transform the object's coordinates, possibly using TPoint.transform() calls.
toRelative(origin), toAbsolute(origin) can rely on the corresponding TPoint methods.
contains(obj), within(obj), intersects(obj) could work on the mbr() of the objects, calling the native corresponding TRect methods; otherwise, a dedicated method should be implemented.
The doRound() method should round each of the object coordinate to the nearest integer.

Please note that, for "unknown" geometric objects, TPoint and TRect within(obj) method call obj.contains(this), while TRect contains(obj) calls obj.within(this).

The className property has to be implemented for all new classes.


JS-GIS © 2001, 2002 by Luca S. Percich (mailto:luca.percich@reacoop.it)
Last updated: dec 26, 2002