public static bool Intersect(this Rect _r, Vector2 p1, Vector2 p2, out Vector2 point) { bool result = false; point = new Vector2(); if (Math2d.LineSegmentsIntersection(p1, p2, _r.position, _r.position + Vector2.up * _r.height, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position, _r.position + Vector2.right * _r.width, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position + Vector2.up * _r.height, _r.position + _r.size, out point)) { result = true; p2 = point; } if (Math2d.LineSegmentsIntersection(p1, p2, _r.position + Vector2.right * _r.width, _r.position + _r.size, out point)) { result = true; p2 = point; } point = p2; return(result); }
void InstIntersections(int startId) { int id = startId + 1; for (int i = 0; i < tracks.Length; i++) { for (int j = i + 1; j < tracks.Length; j++) { for (int x = 1; x < tracks[i].lineRenderer.positionCount; x++) { for (int y = 1; y < tracks[j].lineRenderer.positionCount; y++) { if (Math2d.LineSegmentsIntersection(tracks[i].lineRenderer.GetPosition(x - 1), tracks[i].lineRenderer.GetPosition(x), tracks[j].lineRenderer.GetPosition(y - 1), tracks[j].lineRenderer.GetPosition(y), out Vector2 intersection)) { GameObject intersectionObj = Instantiate(intersectionPrefab); intersectionObj.transform.parent = transform; intersectionObj.transform.position = new Vector3(intersection.x, intersection.y, 1); intersectionObj.GetComponent <Intersection>().Init(id, tracks[i].lineRenderer, tracks[j].lineRenderer, i, j, x - 1, y - 1); id++; intersectionsObjs.Add(intersectionObj); } } } } } }
// Update is called once per frame void Update() { if (originalSatelite.Count < 1) { return; } if (count2 >= originalSatelite.Count | (((100 - ((pastWaight * -1) / originalSatelite.Count) * 100) < 10) && count2 >= 20)) { StateOfMachine.Instance.SetSate = true; count2 = 0; } if (satelite.Count < 1) { if (wight < pastWaight) { bestObstions = new Vector3[lr.positionCount]; lr.GetPositions(bestObstions); nr_of_points_send += bestObstions.Length; nr_of_paths += 1; FindObjectOfType <GameServer>().SendPoint(bestObstions); pastWaight = wight; Satalite_manager.gameObject.SetActive(true); } pointFound.Clear(); pointFound.Add(originalSatelite[count2]); count = 0; wight = -originalSatelite.Count - 1; satelite = new List <Vector3>(originalSatelite); count2++; } else { Vector3 temp = CheckDistance(satelite, pointFound[pointFound.Count - 1]); pointFound.Add(temp); satelite.Remove(temp); for (int i = 0; i < pointFound.Count - 3; i += 2) { if (Math2d.LineSegmentsIntersection(pointFound[pointFound.Count - 2], pointFound[pointFound.Count - 1], pointFound[i], pointFound[i + 1], out Vector)) { wight += 1f; } } lr.positionCount = count + 1; lr.SetPosition(count, temp); count++; } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "ConnectLine") { ConnectLine otherLine = collision.GetComponent <ConnectLine>(); if (otherLine) { //交点を求める. Vector2 intersection; if (Math2d.LineSegmentsIntersection(this.point1.position, this.point2.position, otherLine.point1.position, otherLine.point2.position, out intersection)) { Instantiate(this.CrossedEffect, intersection, Quaternion.identity); } } } }
public bool Intersect(Vector2 p1, Vector2 p2, out List <Vector2> intersection) { intersection = new List <Vector2>(); Vector2 r; if (Math2d.LineSegmentsIntersection(p1, p2, rect.position, rect.position + Vector2.up * rect.height, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position, rect.position + Vector2.right * rect.width, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position + rect.size, rect.position + Vector2.up * rect.height, out r)) { intersection.Add(r); } if (Math2d.LineSegmentsIntersection(p1, p2, rect.position + rect.size, rect.position + Vector2.right * rect.width, out r)) { intersection.Add(r); } return(intersection.Count != 0); }