[Closed] little angle math problem
So I have a closed shape and need to draw a line in an outward direction splitting the angel.
I’m almost there but not sure how to find out what direction to draw my line down my new vector.
In my code I’m getting 2 normalized vectors from 3 knot pos. Then putting a point at a distance from my middle knot down the 2 vectors. Then getting a vector from the 2 new points and splitting the distance. From there I draw a line in red from the middle knot to my new pos. The only problem is I don’t know what direction to draw the line from that knot.
clearListener()
Spline_Obj = $Ground_Spline
offset = 3.5
KnotCount = numKnots Spline_Obj 1
for i = 1 to KnotCount do(
local Before = 0
local After = 0
if i == 1 do(
Before = i + 1
After = KnotCount
)
if i > 1 and i < KnotCount do(
Before = i + 1
After = i - 1
)
if i == KnotCount do(
Before = i - 1
After = 1
)
local Pos_L1_1 = getKnotPoint Spline_Obj 1 i
local Pos_L1_2 = getKnotPoint Spline_Obj 1 Before
local Pos_L1_3 = getKnotPoint Spline_Obj 1 After
local dir1 = normalize (Pos_L1_1 - Pos_L1_2)
local dir2 = normalize (Pos_L1_1 - Pos_L1_3)
local EndPoint1 = Pos_L1_1 + (offset * dir1)
local EndPoint2 = Pos_L1_1 + (offset * dir2)
local dir3 = normalize (EndPoint1 - EndPoint2)
local Line_P2 = EndPoint1 - (((distance EndPoint1 EndPoint2) / 2) * dir3)
local Line_P1 = Pos_L1_1
local ss = SplineShape pos:Line_P1
addNewSpline ss
addKnot ss 1 #corner #line Line_P1
addKnot ss 1 #corner #line Line_P2
updateShape ss
ss.wirecolor = red
ss.name = i as string
)
I was hoping to just get an angle and if the angle was bigger than 180 I would change the direction. But getting the angle from 2 points in below code. Just spits out junk or might be world space?
V1 = Pos_L1_1 - Pos_L1_2
V2 = Pos_L1_3 - Pos_L1_2
N1 = normalize V1
N2 = normalize V2
MyAngle = acos (dot N1 N2)
print ("-------Knot "+ i as string +" ---------")
print MyAngle as string
Any ideas?
lets say you have three points A, B, and C. you have two vectors CA and CB. to get vector you want:
CD = – (normalized (normalized CA + normalized CB))
there is no difference for 2D and 3D in this case
delete objects
(
sp0 = line wirecolor:yellow
s = addnewspline sp0
A = random -[10,10,0] [10,10,0]
B = random -[10,10,0] [10,10,0]
C = [0,0,0]
addknot sp0 s #corner #line A
addknot sp0 s #corner #line C
addknot sp0 s #corner #line B
updateshape sp0
sp1 = line wirecolor:red
s = addnewspline sp1
addknot sp1 s #corner #line C
D = -(normalize (normalize (A-C) + normalize (B-C))) * 10
addknot sp1 s #corner #line D
updateshape sp1
)
This is what I get with my code. I have added the spline shape in the code
clearListener()
offset = 3.5
Spline_Obj = line wirecolor:yellow
s = addnewspline Spline_Obj
addknot Spline_Obj s #corner #line D
addknot Spline_Obj s #corner #line [-9.61374,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,2.80571,0]
addknot Spline_Obj s #corner #line [8.28332,2.80571,0]
addknot Spline_Obj s #corner #line [8.28331,-2.58734,0]
close Spline_Obj 1
updateshape Spline_Obj
KnotCount = numKnots Spline_Obj 1
for i = 1 to KnotCount do(
local Before = 0
local After = 0
if i == 1 do(
Before = i + 1
After = KnotCount
)
if i > 1 and i < KnotCount do(
Before = i + 1
After = i - 1
)
if i == KnotCount do(
Before = i - 1
After = 1
)
local Pos_L1_1 = getKnotPoint Spline_Obj 1 i
local Pos_L1_2 = getKnotPoint Spline_Obj 1 Before
local Pos_L1_3 = getKnotPoint Spline_Obj 1 After
local dir1 = normalize (Pos_L1_1 - Pos_L1_2)
local dir2 = normalize (Pos_L1_1 - Pos_L1_3)
local EndPoint1 = Pos_L1_1 + (offset * dir1)
local EndPoint2 = Pos_L1_1 + (offset * dir2)
local dir3 = normalize (EndPoint1 - EndPoint2)
local Line_P2 = EndPoint1 - (((distance EndPoint1 EndPoint2) / 2) * dir3)
local Line_P1 = Pos_L1_1
local ss = SplineShape pos:Line_P1
addNewSpline ss
addKnot ss 1 #corner #line Line_P1
addKnot ss 1 #corner #line Line_P2
updateShape ss
ss.wirecolor = red
ss.name = i as string
)
Changing my code to your code gives me a bit of strange outcome
here is where I added your code.
clearListener()
offset = 3.5
Spline_Obj = line wirecolor:yellow
s = addnewspline Spline_Obj
addknot Spline_Obj s #corner #line D
addknot Spline_Obj s #corner #line [-9.61374,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,2.80571,0]
addknot Spline_Obj s #corner #line [8.28332,2.80571,0]
addknot Spline_Obj s #corner #line [8.28331,-2.58734,0]
close Spline_Obj 1
updateshape Spline_Obj
KnotCount = numKnots Spline_Obj 1
for i = 1 to KnotCount do(
local Before = 0
local After = 0
if i == 1 do(
Before = i + 1
After = KnotCount
)
if i > 1 and i < KnotCount do(
Before = i + 1
After = i - 1
)
if i == KnotCount do(
Before = i - 1
After = 1
)
local B = getKnotPoint Spline_Obj 1 i
local A = getKnotPoint Spline_Obj 1 Before
local C = getKnotPoint Spline_Obj 1 After
sp1 = line wirecolor:red
s = addnewspline sp1
addknot sp1 s #corner #line C
D = -(normalize (normalize (A-C) + normalize (B-C))) * 10
addknot sp1 s #corner #line D
updateshape sp1
)
what did I do wrong? So all the lines should draw away from the shape
For any non-convex polygon, you must use an algorithm which takes into consideration which side of the line you were offsetting the previous point to. It’s impossible to know just from 3 points which direction your normal should face.
Yes.
That’s why may question in “2D”.
Knowing that, I can check if the triedrium points up or down (rotvec.z in my code).
Here you are AngryBear:
clearListener()
--Spline_Obj = $Ground_Spline
Spline_Obj = line wirecolor:yellow
s = addnewspline Spline_Obj
addknot Spline_Obj s #corner #line [-9.61374,-2.58734,0]
addknot Spline_Obj s #corner #line [-9.61374,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,35.0624,0]
addknot Spline_Obj s #corner #line [-6.73387,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,36.4994,0]
addknot Spline_Obj s #corner #line [5.41729,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,35.0624,0]
addknot Spline_Obj s #corner #line [8.28331,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,32.4373,0]
addknot Spline_Obj s #corner #line [9.61373,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,19.3516,0]
addknot Spline_Obj s #corner #line [8.28331,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,15.2587,0]
addknot Spline_Obj s #corner #line [9.61373,2.80571,0]
addknot Spline_Obj s #corner #line [8.28332,2.80571,0]
addknot Spline_Obj s #corner #line [8.28331,-2.58734,0]
close Spline_Obj 1
updateshape Spline_Obj
offset = 3.5
KnotCount = numKnots Spline_Obj 1
for i = 1 to KnotCount do(
in coordsys world
local Before = 0
local After = 0
if i == 1 do(
Before = KnotCount
After = i + 1
)
if i > 1 and i < KnotCount do(
Before = i - 1
After = i + 1
)
if i == KnotCount do(
Before = i - 1
After = 1
)
local Pos_L1_1 = getKnotPoint Spline_Obj 1 i
local Pos_L1_2 = getKnotPoint Spline_Obj 1 Before
local Pos_L1_3 = getKnotPoint Spline_Obj 1 After
local dir1 = normalize (Pos_L1_1 - Pos_L1_2)
local dir2 = normalize (Pos_L1_3 - Pos_L1_1)
local dir3 = normalize (dir2 + dir1)
local rotvector = normalize (cross dir2 dir1)
local dir3_good = normalize (cross rotvector dir3)
if rotvector.z <0.0 do dir3_good = -dir3_good
local Line_P2 = Pos_L1_1 + offset * dir3_good
local Line_P1 = Pos_L1_1
local ss = SplineShape pos:Line_P1
addNewSpline ss
addKnot ss 1 #corner #line Line_P1
addKnot ss 1 #corner #line Line_P2
updateShape ss
ss.wirecolor = green
ss.name = i as string
)
Is there a normal for a spline segment? or do I need to copy extrude and use geo normal dir?
But in your code, there where other problems, as how you assign the ‘after’ and ‘before’ knogts (the last one at least).
And your interpretation of denisT code is bad too.
I’m gonna try but I think it won’t work.