Before continue reading, take a look at the attached image first, as it may help alot in understanding the problem. I have this shape. It is a mesh, created by giving it an outline of vector3’s and triangulate them. That is the initial form.
Now I want to be able to divide it (or create the illusion of division) by drawing lines across it. As you can see in the image, I have the two lines AB and CD. These lines already divided the shape, but these are easy to do because they don’t touch other lines. The line MN is a different story.
But lets ignore the shape for a second, and focus only on the lines. How can I calculate if and/or where the line MN touches the lines AB or CD.
(My ultimate goal is to be able to divide the ‘shape’ at will, later on, add vertex control, both inside and on the edge of the shape, to be able to select the divisions, lines and verteces, and maybe even delete them.)
I don’t think this will work. It only takes in 3 vectors, (I imagined something with at least 4) and it returns a string, not the intercepting point.
Maybe I didn’t explain correctly
you dont need all the code its about the dot product with this you can check if a point is on the right or left side of a line if it is 0 the point is on the line so if your 2 vectors of a line return right and left you cross the line
I’m not following you. My math isn’t that good.
I’ve checked Unity’s dot product and still don’t get it. (Unity - Scripting API: Vector3.Dot)
Can you write some code?
using UnityEngine;
using System.Collections;
public class Dot : MonoBehaviour
{
private class Line
{
public Vector3 point_a ;
public Vector3 point_b ;
public Line(Vector3 a, Vector3 b)
{
this.point_a = new Vector3 (a.x, a.y, a.z);
this.point_b = new Vector3 (b.x, b.y, b.z);
}
}
Line line_a = new Line(new Vector3(5,0,0),new Vector3(5,10,0));// the midle line
Line line_b = new Line(new Vector3(0,5,0),new Vector3(10,0,0));// the line will cross the mid
Line line_c = new Line(new Vector3(0,5,0),new Vector3(4,0,0));// this line is to short
void Start ()
{
float dot_product;
//line_a & line_b
// we compare line_a with point_a of line_b
dot_product = (line_a.point_b.x - line_a.point_a.x) * (line_b.point_a.y - line_a.point_a.y) - (line_a.point_b.y - line_a.point_a.y) * (line_b.point_a.x - line_a.point_a.x);
ReturnState (dot_product);// it retun right
// we compare line_a with point_b of line_b
dot_product = (line_a.point_b.x - line_a.point_a.x) * (line_b.point_b.y - line_a.point_a.y) - (line_a.point_b.y - line_a.point_a.y) * (line_b.point_b.x - line_a.point_a.x);
ReturnState (dot_product);// it retun left
// now we got one point left and one right so the line_b is crossing line_a
//line_a & line_c
// we compare line_a with point_a of line_c
dot_product = (line_a.point_b.x - line_a.point_a.x) * (line_c.point_a.y - line_a.point_a.y) - (line_a.point_b.y - line_a.point_a.y) * (line_c.point_a.x - line_a.point_a.x);
ReturnState (dot_product);// it retun right
// we compare line_a with point_b of line_c
dot_product = (line_a.point_b.x - line_a.point_a.x) * (line_c.point_b.y - line_a.point_a.y) - (line_a.point_b.y - line_a.point_a.y) * (line_c.point_b.x - line_a.point_a.x);
ReturnState (dot_product);// it retun right
// now we got two points on the right side so the lines dont cross
}
private void ReturnState(float dot)
{
if(dot > 0)Debug.Log ("right");
if(dot < 0)Debug.Log ("left");
//if(dot_Product == 0) tmp_String = "on line";
}
The strange part for me is that you only take 1 vector3 for each line. Don’t you need 2? One for the beguining and one for the end? And there is no return of the vector3 of the intercepting point (if there is any)