Point

A **Point** is a class describing the position of a graphic. It has two elements, the *horizontal offset* (in points) and the *vertical offset* (in points) of the object from the canvas origin. A **Point** is also the first component of a Rect, which describes both the location (Point) and dimensions (Size) of a graphic.

Creating a Point

An instance of the **Point** class is created by preceding the class name with the **new** command, and following the class name with the *horizontal offset* (in points), and *vertical offset* (in points) of the object, enclosed in a pair of parens (). The result is a new instance of the **Point** class:

**new Point(horizontalOffset, verticalOffset)**(Point) • Returns a new Point with the specified coordinates.

pt = new Point(72, 90)
// [object Point: (72.0, 90.0)]

omnigraffle:///omnijs-run?script=pt%20%3D%20new%20Point%2872%2C%2090%29

Create a Point | ||

01 | pt = new Point(72, 90) | |

02 | // [object Point: (72.0, 90.0)] |

Class Properties

By default, the Point class has three intrinsic properties for creating a defined point.

**unitX**(Point r/o) • Returns a Point with coordinates (1, 0)**unitY**(Point r/o) • Returns a Point with coordinates (0, 1)**zero**(Point r/o) • Returns the Point (0, 0), the origin.

ptX = Point.unitX
ptY = Point.unitY
pt0 = Point.zero

Point Class Properties | ||

01 | ptX = Point.unitX | |

02 | //[object Point: (1.0, 0.0)] | |

03 | ptY = Point.unitY | |

04 | //[object Point: (0.0, 1.0)] | |

05 | pt0 = Point.zero | |

06 | //[object Point: (0.0, 0.0)] |

Instance Properties

An instance of the **Point** class has five properties which can be accessed by scripts.

**length**(Number r/o) • Returns the distance between the receiver and the origin.

pt = new Point(200, 200)
pt.length
//282.842712474619

The Length of a Point | ||

01 | pt = new Point(200, 200) | |

02 | pt.length | |

03 | //282.842712474619 |

The value of the **length** property is the **Point**’s distance from the canvas *origin*. In essence, this value is length of the hypotenuse of the right triangle formed by the horizontal and vertical offsets of the point. It is determined using the Pythagorean theorem.

**negative**(Number r/o) • Returns the component-wise negative of the receiver.

pt = new Point(100, 200)
pt.negative

The Negative of a Point | ||

01 | pt = new Point(100, 200) | |

02 | pt.negative | |

03 | //[object Point: (-100.0, -200.0)] |

**normalized**(Number r/o) • For a non-zero point, returns a point with a distance of one from the origin, but in the same direction as the original. For the zero point, this returns the receiver.

pt = new Point(100, 200)
pt.normalized

Normalized Point | ||

01 | pt = new Point(100, 200) | |

02 | pt.normalized | |

03 | //[object Point: (0.447213595499958, 0.894427190999916)] |

**x**(Number r/o) • The horizontal axis coordinate of the Point.

pt = new Point(100, 200)
pt.x

The Horiontal Component of a Point | ||

01 | pt = new Point(100, 200) | |

02 | pt.x | |

03 | //100 |

**y**(Number r/o) • The verticql axis coordinate of the Point.

pt = new Point(100, 200)
pt.x

The Horiontal Component of a Point | ||

01 | pt = new Point(100, 200) | |

02 | pt.y | |

03 | //200 |

Instance Functions

There are three functions you can use to derive a new point based upon an instance of a point, and two functions for deriving a measurement from a point instance.

**add(Point)**(Point) • Returns a new Point that is the component-wise sum of the receiver and the argument Point**subtract(Point)**(Point) • Returns a new Point that is the component-wise difference of the receiver and the argument Point**scale(Point)**(Point) • Returns a new Point where each component is scaled by the given factor.**distanceTo(Point)**(Number) • Returns the distance between the receiver and the given Point**dot(Point)**(Number) • Returns the dot product between the receiver and the given Point.

Here’s an example script that uses the **add()** instance function with the **duplicateTo()** graphic function to create a diagonal row of squares:

cnvs = document.windows[0].selection.canvas
side = 100
pt = new Point(side,side)
shape = cnvs.addShape('Square',new Rect(0,0,side,side))
for(i = 0; i < 3; i++){
shape = shape.duplicateTo(shape.geometry.origin.add(pt))
}

omnigraffle:///omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Aside%20%3D%20100%0Apt%20%3D%20new%20Point%28side%2Cside%29%0Ashape%20%3D%20cnvs%2EaddShape%28%27Square%27%2Cnew%20Rect%280%2C0%2Cside%2Cside%29%29%0Afor%28i%20%3D%200%3B%20i%20%3C%203%3B%20i%2B%2B%29%7B%0A%09shape%20%3D%20shape%2EduplicateTo%28shape%2Egeometry%2Eorigin%2Eadd%28pt%29%29%0A%7D

Adding Points | ||

01 | cnvs = document.windows[0].selection.canvas | |

02 | side = 100 | |

03 | pt = new Point(side,side) | |

04 | shape = cnvs.addShape('Square',new Rect(0,0,side,side)) | |

05 | for(i = 0; i < 3; i++){ | |

06 | shape = shape.duplicateTo(shape.geometry.origin.add(pt)) | |

07 | } |

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.