class Point

Copyright © 2001 by Jim Menard <jimm@io.com>

Released under the same license as Ruby. See www.ruby-lang.org/en/LICENSE.txt.

Constants

ORIGIN

Attributes

x[RW]
y[RW]
z[RW]

Public Class Methods

midpoint(a, b) click to toggle source

Return a new Point that is the midpoint on the line between two points.

# File examples/ruboids/ruboids/Point.rb, line 14
def Point.midpoint(a, b)
    return Point.new((a.x + b.x) * 0.5, (a.y + b.y) * 0.5,
                       (a.z + b.z) * 0.5)
end
new(x = 0, y = 0, z = 0) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 19
def initialize(x = 0, y = 0, z = 0)
    if x.kind_of?(Point)
        @x = x.x
        @y = x.y
        @z = x.z
    else
        @x = x
        @y = y
        @z = z
    end
end

Public Instance Methods

==(point) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 33
def ==(point)
    return point.kind_of?(Point) &&
        @x == point.x && @y == point.y && @z == point.z
end
add(d) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 87
def add(d)
    @x += d
    @y += d
    @z += d
    return self
end
addPoint(p) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 94
def addPoint(p)
    @x += p.x
    @y += p.y
    @z += p.z
    return self
end
crossProduct(p) click to toggle source

Return a new point that is the cross product of this point and another. The cross product of two unit vectors is another vector that's at right angles to the first two (for example, a surface normal).

# File examples/ruboids/ruboids/Point.rb, line 58
def crossProduct(p)
    return Point.new(@y * p.z - @z * p.y, @z * p.x - @x * p.z,
                     @x * p.y - @y * p.x)
end
distanceTo(p) click to toggle source

Return distance between this point and another.

# File examples/ruboids/ruboids/Point.rb, line 80
def distanceTo(p)
    dx = p.x - @x
    dy = p.y - @y
    dz = p.z - @z
    return Math.sqrt(dx * dx + dy * dy + dz * dz)
end
divideBy(d) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 131
def divideBy(d)
    @x = @x / d
    @y = @y / d
    @z = @z / d
    return self
end
divideByPoint(p) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 138
def divideByPoint(p)
    @x = @x / p.x
    @y = @y / p.y
    @z = @z / p.z
    return self
end
dotProduct(p) click to toggle source

Return the (scalar) dot product of this vector and another. The dot product of two vectors produces the cosine of the angle between them, multiplied by the lengths of those vectors. (The dot product of two normalized vectors equals cosine of the angle.)

# File examples/ruboids/ruboids/Point.rb, line 67
def dotProduct(p)
    return @x * p.x + @y * p.y + @z * p.z
end
multiplyBy(d) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 117
def multiplyBy(d)
    @x *= d
    @y *= d
    @z *= d
    return self
end
multiplyByPoint(p) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 124
def multiplyByPoint(p)
    @x *= p.x
    @y *= p.y
    @z *= p.z
    return self
end
normalize() click to toggle source

Return a new point that is a normalized version of this point.

# File examples/ruboids/ruboids/Point.rb, line 51
def normalize
    return self.dup().normalize!()
end
normalize!() click to toggle source

Normalize this point.

# File examples/ruboids/ruboids/Point.rb, line 39
def normalize!
    mag = @x * @x + @y * @y + @z * @z
    if mag != 1.0
        mag = 1.0 / Math.sqrt(mag)
        @x *= mag
        @y *= mag
        @z *= mag
    end
    return self
end
squareOfDistanceTo(p) click to toggle source

Return square of distance between this point and another.

# File examples/ruboids/ruboids/Point.rb, line 72
def squareOfDistanceTo(p)
    dx = p.x - @x
    dy = p.y - @y
    dz = p.z - @z
    return dx * dx + dy * dy + dz * dz
end
subtract(d) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 102
def subtract(d)
    @x -= d
    @y -= d
    @z -= d
    return self
end
subtractPoint(p) click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 109
def subtractPoint(p)
    @x -= p.x
    @y -= p.y
    @z -= p.z
    return self
end
to_a() click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 145
def to_a
    return [@x, @y, @z]
end
to_s() click to toggle source
# File examples/ruboids/ruboids/Point.rb, line 149
def to_s
    return "Point<#{@x}, #{@y}, #{@z}>"
end