class Point

Attributes

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

Public Class Methods

new(x, y, z) click to toggle source
# File lib/geometry_3d/point.rb, line 6
def initialize(x, y, z)
  @x = x
  @y = y
  @z = z
end

Public Instance Methods

==(point) click to toggle source
# File lib/geometry_3d/point.rb, line 106
def ==(point)
  @x == point.x and @y == point.y and @z == point.z
end
distance_to_line(line) click to toggle source

Finds the distance between point and line.

Example:

>> Point.new(0, 2, 3).distance_to_line(Line.new(Point.new(3, 1, -1), Vector.new(2, 1, 2)))
=> 5

Arguments:

line: (Line)
# File lib/geometry_3d/point.rb, line 72
def distance_to_line(line)
  numer = Vector.construct_with_two_points(self, line.point).cross_product(line.vector)
  numer.length / line.vector.length
end
distance_to_plane(plane) click to toggle source

Finds the distance between point and plane.

Example:

>> Point.new(4, -4, 3).distance_to_plane(Plane.new(2, -2, 5, 8))
=> 6.789028582272215

Arguments:

plane: (Plane)
# File lib/geometry_3d/point.rb, line 85
def distance_to_plane(plane)
  plane.substitute(self).abs / (plane.find_normal_vector.length)
end
distance_to_point(point) click to toggle source

Finds the distance between two points.

Example:

>> Point.new(-7, -4, 3).distance_to_point(Point.new(17, 6, 2.5))
=> 26.004807247891687

Arguments:

point: (Point)
# File lib/geometry_3d/point.rb, line 60
def distance_to_point(point)
  to_a.zip(point.to_a).map{|a, b| (b - a) ** 2}.reduce(:+) ** 0.5
end
get_midpoint(point) click to toggle source

Finds the midpoint between two points.

Example:

>> Point.new(-7, -4, 3).get_midpoint(Point.new(17, 6, 2.5))
=> Point.new(5, 1, 2.75)

Arguments:

point: (Point)
# File lib/geometry_3d/point.rb, line 98
def get_midpoint(point)
  Point.new(*to_a.zip(point.to_a).map { |a, b| (a + b) / 2.0 })
end
is_on_line?(line) click to toggle source

Check if a point is on a line

Example:

>> Point.new(1, 1, 4).is_on_line?(Line.new(Point.new(2, -1, 3), Vector.new(-1, 2, 1)))
=> true

Arguments:

line: (Line)
# File lib/geometry_3d/point.rb, line 47
def is_on_line?(line)
  to_a.zip(line.point.to_a, line.vector.to_a).map { |a, b, c| (a - b) / c }.uniq.length == 1
end
is_on_plane?(plane) click to toggle source

Check if a point is on a plane

Example:

>> Point.new(1, 1, 1).is_on_plane?(Plane.new(1, 2, 3, -6))
=> true

Arguments:

plane: (Plane)
# File lib/geometry_3d/point.rb, line 34
def is_on_plane?(plane)
  plane.substitute(self) == 0
end
round(number) click to toggle source
# File lib/geometry_3d/point.rb, line 110
def round(number)
  Point.new(*to_a.map{|x| x.round(number)})
end
to_a() click to toggle source
# File lib/geometry_3d/point.rb, line 102
def to_a
  [x, y, z]
end
translate(vector) click to toggle source

Translate a point with a vector

Example:

>> Point.new(2, -1, 3).translate(Vector.new(1, 2, 3))
=> Point.new(3, 1, 6)

Arguments:

vector: (Vector)
# File lib/geometry_3d/point.rb, line 21
def translate(vector)
  Point.new(*to_a.zip(vector.to_a).map { |a, b| a + b})
end