Points and directionality
-
A user clicks on 2 points and I create a door. Unfortunately if the user picks the right corner and drags to the left corner then my door gets created backwards. My problem is to detect which of the 2 points is the most left based on the current view. I have the bottom 2 points but I do not know which is right and which is left.
One strategy that I see is maybe I could use the camera's eye
Sketchup.active_model.active_view.camera.eye
I could create 2 vectors and then get the angle between the vectors.
But will this allow me to determine which is the most left? -
Would you not want to create the door in the direction the user picks the points?
Do you mean left/right in the viewport sense - or left/right in the sense of the 3d model space?
-
@tt_su said:
Do you mean left/right in the viewport sense - or left/right in the sense of the 3d model space?
viewport sense - I think that's where the camera might help out.
-
I assume you are flattening the two points so
pt2.z = pt1.z
?
And trapping forpt1.vector_to(pt2).parallel?(Z_AXIS)
And trapping forpt1==pt2
etc...So now you have two coplanar points pt1 & pt2.
You always take the insertion-point as the left-hand-most-point and then the direction for the door from the vector of that insertion-point to the other-point.
eye = Sketchup.active_model.active_view.camera.eye
Then
ve1 = eye.vector_to(pt1) ve2 = eye.vector_to(pt2)
The check which point is "to the left using"
if ve1.parallel?(ve2) if ve2.length > ve1.length # nearest-point is insertion p1=pt1 p2=pt2 else p1=pt2 p2=pt1 end else # find ordering of points left/right cross = vec1.cross(ve2) if cross.z <= 0 p1=pt1 p2=pt2 elsif cross.z > 0 p1=pt2 p2=pt1 end end
Now use p1 & p2 for you insertion-point and direction calcs...
NOT pt1 & pt2 which might be swapped ?? -
Thanks TIG - I'll chew on this for awhile and see where I get.
This line confuses me - I'm thinking a couple of typos??cross = vec1.cross(ve2)[/ruby
did you mean
cross = ve1.cross(ve2) #ruby
-
cross = ve1.cross(ve2)
The other bit was a typo edit balls up !
I've removed it from my original text for the avoidance of confusion...If you 'cross' two vectors you get a vector at right-angles to them.
It points 'up' [>0] or 'down' [<0] depending of the clockwise/ccw order of the vectors.
If you make the two points coplanar, with the same z, then it's either [0,0,-1] or [0,0,1] depending on the 'order' cw/ccw etc... -
TIG,
I didn't need the parallel code. I wanted to return the original pts and the flattened one
pt1 = @ip1.position pt2 = @ip2.position p1 = Geom;;Point3d.new(pt1.x, pt1.y, [pt1.z, pt2.z].min) p2 = Geom;;Point3d.new(pt2.x, pt2.y, [pt1.z, pt2.z].min) eye = Sketchup.active_model.active_view.camera.eye ve1 = eye.vector_to(p1) ve2 = eye.vector_to(p2) return [pt2,pt1,p2] if( ve1.cross(ve2).z > 0 ) [pt1,pt2,p1]
Advertisement