- Home /
Create line that splits triangle
I have three joints that make up an arm. They can be in any position.
I find the angle of the middle point using the Law of Cosines. For example in these examples it would be around 115 degrees or so. http://en.wikipedia.org/wiki/Law_of_cosines
I now need to draw 2 lines (one in each direction) that are half of that angle, so they basically split the triangle in half. Like this.
I found a thread about creating lines at angles here, so I'm using similar code. http://answers.unity3d.com/questions/350971/how-to-project-an-angle-from-a-vector.html
Now as a test here is my code shooting lines at 0 and 180 and I get this. Seemes pretty odd that they aren't straight up and down.....
pointer.transform.position = (Quaternion.Euler( 0, 0, 0) * bone_lelbow_lhand.transform.up) * range;
pointer2.transform.position = (Quaternion.Euler( 0, 0, -180) * bone_lelbow_lhand.transform.up) * range;
Debug.DrawLine(bone_lelbow_lhand.transform.position, pointer.transform.position, Color.green, 100, true);
Debug.DrawLine(bone_lelbow_lhand.transform.position, pointer2.transform.position, Color.blue, 100, true);
Anyone have any ideas? I have a feeling I need to use atan2 or align transform.position.up with one of the other joints so the angle appears relative?
Let me make sure I understand. You are trying to draw the red line. You want the red line to bisect the angle formed by the three points. You want a line segment running in each direction through the middle point. Correct?
Answer by robertbu · Jun 18, 2013 at 09:32 PM
My first though about solving this problem would be to use the cross product. Here is a bit of code:
var trA : Transform;
var trB : Transform;
var trC : Transform;
function Update() {
Debug.DrawLine(trB.position, trA.position, Color.green);
Debug.DrawLine(trB.position, trC.position, Color.green);
var v3BA = trA.position - trB.position;
var v3BC = trC.position - trB.position;
var axis = Vector3.Cross(v3BA, v3BC);
var angle = Vector3.Angle(v3BA, v3BC);
var v3 = Quaternion.AngleAxis(angle / 2.0, axis) * v3BA;
Debug.DrawRay(trB.position, v3);
Debug.DrawRay(trB.position, -v3);
}
Attach this script to an empty game object. Put three game objects in the scene and drag and drop them onto the trA, trB, and trC variables. Run the script. You can then move the object around in the inspector while the script is running.
You could also use the Angle Bisector Theorem to find point D on line segment AC where the bisector will intersect. Another example script:
var trA : Transform;
var trB : Transform;
var trC : Transform;
function Update() {
Debug.DrawLine(trB.position, trA.position, Color.green);
Debug.DrawLine(trB.position, trC.position, Color.green);
var v3BA = trA.position - trB.position;
var v3BC = trC.position - trB.position;
var v3AC = trC.position - trA.position;
var v3D = trA.position + v3AC * v3BA.magnitude / (v3BA.magnitude + v3BC.magnitude);
var v3 = v3D - trB.position;
Debug.DrawRay(trB.position, v3);
Debug.DrawRay(trB.position, -v3);
}
Hell yeah! Awesome answer, both of those work flawlessly. Thanks a lot! I wish I could up vote this a billion times!
Your answer
![](https://koobas.hobune.stream/wayback/20220613104425im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Angle between two lines in 2D 3 Answers
Trying to obtain sine/cosine by angles giving me imprecise values 1 Answer
Basic vector math addition 1 Answer
Draw a Vector line ??? 3 Answers
How would I calculate the number of lines a string will be 3 Answers