Class: Silicium::Geometry3d::Plane3d

Inherits:
Object
  • Object
show all
Defined in:
lib/geometry3d.rb

Overview

Class represents a plane as equation Ax + By + Cz+D = 0 in two-dimensional space

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(point1, point2, point3) ⇒ Plane3d

Initializes with three objects of type Point



29
30
31
32
33
34
35
36
# File 'lib/geometry3d.rb', line 29

def initialize(point1, point2, point3)
  vector1 = Vector3d.new(point1)
  norm = vector1.norm_vector(point2, point3)
  @x_coefficient = norm.x
  @y_coefficient = norm.y
  @z_coefficient = norm.z
  @free_coefficient = -point1.x * norm.x + (-point1.y * norm.y) + (-point1.z * norm.z)
end

Instance Attribute Details

#free_coefficientObject (readonly)

Returns the value of attribute free_coefficient.



26
27
28
# File 'lib/geometry3d.rb', line 26

def free_coefficient
  @free_coefficient
end

#x_coefficientObject (readonly)

Returns the value of attribute x_coefficient.



23
24
25
# File 'lib/geometry3d.rb', line 23

def x_coefficient
  @x_coefficient
end

#y_coefficientObject (readonly)

Returns the value of attribute y_coefficient.



24
25
26
# File 'lib/geometry3d.rb', line 24

def y_coefficient
  @y_coefficient
end

#z_coefficientObject (readonly)

Returns the value of attribute z_coefficient.



25
26
27
# File 'lib/geometry3d.rb', line 25

def z_coefficient
  @z_coefficient
end

Instance Method Details

#distance_between_parallel_planes(other_plane) ⇒ Object

The distance between parallel planes



90
91
92
93
94
95
# File 'lib/geometry3d.rb', line 90

def distance_between_parallel_planes(other_plane)
  raise 'Planes are not parallel' if !parallel?(other_plane)

  free = (other_plane.free_coefficient - @free_coefficient).abs
  free / sqrt(@x_coefficient**2 + @y_coefficient**2 + @z_coefficient**2)
end

#distance_point_to_plane(point) ⇒ Object

The distance from a point to a plane



100
101
102
103
104
# File 'lib/geometry3d.rb', line 100

def distance_point_to_plane(point)
  norm = 1 / Math.sqrt(@x_coefficient**2 + @y_coefficient**2 + @z_coefficient**2)
  (@x_coefficient * norm * point.x + @y_coefficient * norm * point.y +
      @z_coefficient * norm * point.z + @free_coefficient * norm).abs
end

#initialize_with_coefficients(a, b, c, d) ⇒ Object

Initializes with coefficients

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
# File 'lib/geometry3d.rb', line 40

def initialize_with_coefficients(a, b, c, d)
  raise ArgumentError, 'All coefficients cannot be 0 ' if a.equal?(0) && b.equal?(0) && c.equal?(0) && (d.equal?(0) || !d.equal?(0))

  @x_coefficient = a
  @y_coefficient = b
  @z_coefficient = c
  @free_coefficient = d
end

#intersecting?(other_plane) ⇒ Boolean

Checks if two planes are intersecting in 3-dimensional space

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/geometry3d.rb', line 72

def intersecting?(other_plane)
  check_x = @x_coefficient != other_plane.x_coefficient
  check_y = @y_coefficient != other_plane.y_coefficient
  check_z = @z_coefficient != other_plane.z_coefficient
  check_x || check_y || check_z
end

#parallel?(other_plane) ⇒ Boolean

Checks if two planes are parallel in 3-dimensional space

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/geometry3d.rb', line 64

def parallel?(other_plane)
  v1 = Vector3d.new(Point3d.new(@x_coefficient, @y_coefficient, @z_coefficient))
  v2 = Vector3d.new(Point3d.new(other_plane.x_coefficient, other_plane.y_coefficient, other_plane.z_coefficient))
  v1.collinear?(v2)
end

#perpendicular?(other_plane) ⇒ Boolean

Checks if two planes are perpendicular

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/geometry3d.rb', line 81

def perpendicular?(other_plane)
  check_x = @x_coefficient * other_plane.x_coefficient
  check_y = @y_coefficient * other_plane.y_coefficient
  check_z = @z_coefficient * other_plane.z_coefficient
  (check_x + check_y + check_z).equal?(0)
end

#point_is_on_line?(point1, point2, point3) ⇒ Boolean

check if the points isn’t on the same line

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/geometry3d.rb', line 51

def point_is_on_line?(point1, point2, point3)
  check_p1 = @x_coefficient * point1.x + @y_coefficient * point1.y + @z_coefficient * point1.z +  @free_coefficient
  check_p2 = @x_coefficient * point2.x + @y_coefficient * point2.y + @z_coefficient * point2.z + @free_coefficient
  check_p3 = @x_coefficient * point3.x + @y_coefficient * point3.y + @z_coefficient * point3.z + @free_coefficient
  check_p1.equal?(0) && check_p2.equal?(0) && check_p3.equal?(0)
end

#point_is_on_plane?(point) ⇒ Boolean

check if the point isn’t on the plane

Returns:

  • (Boolean)


59
60
61
# File 'lib/geometry3d.rb', line 59

def point_is_on_plane?(point)
  (@x_coefficient * point.x + @y_coefficient * point.y + @z_coefficient * point.z + @free_coefficient).equal?(0)
end