Vector parallel to two points

Hooray to BruceYoung!

Here is the slightly more flexible demonstration of what he has just said.

##############
# Given the definition of a (Sketchup) circle and an arbitrary chord of that circle
# - add a linear dimension that is on the plane of the circle
# - parallel to the chord
# - and offset to the outside of the circle

model = Sketchup.active_model
ents = model.active_entities
model.start_operation('Dimension Chord', true)

  # define a circle
  radius = 10
  num_segs = 48
  centerpoint = [0, 0, 0]
  circle_normal = Geom::Vector3d.new 0,1,0
  edges = ents.add_circle(centerpoint, circle_normal, radius, numsegs = num_segs)

  # choose a chord at random from the vertices of the circle
  i1 = rand(num_segs)
  i2 = rand(num_segs - 1 ) + 1
  i3 = (i1 + i2) % num_segs

  a = edges[i1].start.position
  b = edges[i3].start.position

  # draw the chord between the two points
  ents.add_edges([a, b])
  
  ######################
  # Add a dimension that is offset from the chord and on the plane of the circle
  
  # The cross product of a normal(a vector) and an edge(a vector) yields a third vector
  # which is perpendicular to the plane defined by the first two vectors
  offset_vector = circle_normal.cross(a.vector_to(b)) 

  # scale the offset vector relative to length of the radius
  offset_vector.length = radius / 5.0
  
  # reverse the offset vector if it is pointing to the interior of the circle
  radius_to_a = centerpoint.vector_to(a)
  length = (radius_to_a + offset_vector).length
  if length < radius
      offset_vector = Geom::Vector3d.new(0,0,0) - offset_vector
  end
  
  # add the dimension
  ents.add_dimension_linear( a, b, offset_vector)

model.commit_operation

1 Like