Skip to content
Examples

Points and vectors

Build points and vectors, combine them, and show them in COMPAS Viewer

Points are locations, vectors are directions with a length. This example builds both, uses the cross product to find a perpendicular direction, and shows everything as a point and lines. It needs compas_viewer; see Making a box to install it.

The script

points_and_vectors.py
from compas.geometry import Line, Point, Vector
from compas_viewer import Viewer

origin = Point(0, 0, 0)
u = Vector(1, 0, 0)
v = Vector(1, 1, 0)

w = u.cross(v)      # perpendicular to u and v
angle = u.angle(v)  # radians
print(w, angle)

viewer = Viewer()
viewer.scene.add(origin)
for vector in (u, v, w):
    viewer.scene.add(Line(origin, origin + vector))
viewer.show()

A point plus a vector gives a point; a point minus a point gives a vector. cross returns a vector perpendicular to both inputs, here the world Z axis. dot, angle and length cover the rest of everyday vector maths.