Пример #1
0
	private static void GetIntersectionPoint(Vector2 center, Vector2 originDirection, IEnumerable<KeyValuePair<SideType,MathLine>> sides, MathLine inputLine, out SideType intersectionSide, out Vector2 intersectionPoint)
	{
		intersectionSide = SideType.None;
		intersectionPoint = new Vector2(Mathf.Infinity, Mathf.Infinity);

		float sqrDistance = Mathf.Infinity;
		
		foreach (var side in sides)
		{
			SideType sideType = side.Key;
			MathLine line = side.Value;
			
			Vector2 point = line.GetIntersectionPoint(inputLine.Coefficients);
			Vector2 direction = point - center;
			float distance = Vector2.SqrMagnitude(direction);
			
			if (distance <= sqrDistance && Vector2.Dot(direction, originDirection) > 0)
			{
				sqrDistance = distance;
				intersectionSide = sideType;
				intersectionPoint = point;
			}
		}
	}
Пример #2
0
	public void DrawLine()
	{
		IsEnabled = true;

		// Cleanup old Line
		VectorLine.Destroy(ref _line);
		
		// Create new Line
		_line = new VectorLine("Line", new List<Vector2>(), null, LineWidth, LineType.Discrete, Joins.Weld);
		_line.color = LineColor;
		_line.drawDepth = LineOrder;

		Vector2 lastPoint = Vector2.zero;
		// Draw all segments and connection lines
		for(var i = 0; i < Cells.Length; i++)
        {
			var cell = Cells[i];
			// Part 0
			// Add Line's Head
			if (i == 0)
			{
				lastPoint = GetLineStart(cell);
			}

			// Part 1
			_line.points2.Add(lastPoint);
			MathLine line = new MathLine().CalculateLineByPoints(lastPoint, cell.Center);
			SideType side = SideType.None;

			if (cell.Visible)
			{
				cell.GetExternalIntersectionPoint(line, cell.Center, lastPoint - cell.Center, 0f, out side, out lastPoint);
			}
			else
			{
				// Prepare line for Stump
				cell.GetExternalIntersectionPoint(line, cell.Center, lastPoint - cell.Center, LineWidth / 3f, out side, out lastPoint);
			}
			_line.points2.Add(lastPoint);

			// Part 2
			if (cell.Visible)
			{
				// Draw line through cell center
				_line.points2.Add(lastPoint);
				_line.points2.Add(cell.Center);
			}
			else
			{
				// Draw Stump to have nice line ending
				_line.points2.Add(lastPoint);
				_line.points2.Add(lastPoint + LineWidth / 4f * GetStumpVector(side));
			}

			// Part 3
			Vector2 nextPoint;
			// If current point is the last one, take Right Cell's point
			if (i == Cells.Length - 1)
			{
				nextPoint = GetLineEnding(cell);
			}
			// Otherwise take next Cell's center as point
			else 
			{
				Cell nextCell = Cells[i + 1];
				nextPoint = nextCell.Center;
			}
			line = new MathLine().CalculateLineByPoints(cell.Center, nextPoint);

			if (cell.Visible)
            {
				cell.GetInternalIntersectionPoint(line, cell.Center, nextPoint - cell.Center, 0f, out side, out lastPoint);
				// Start line from Cell's center
				_line.points2.Add(cell.Center);
				_line.points2.Add(lastPoint); 
			}
			else
			{
				cell.GetInternalIntersectionPoint(line, cell.Center, nextPoint - cell.Center, LineWidth / 3f, out side, out lastPoint);
				// Draw Stump
				_line.points2.Add(lastPoint + LineWidth / 4f * GetStumpVector(side));
				_line.points2.Add(lastPoint);
			}

			// Part 4
			// Add Line tail
			if (i == Cells.Length - 1)
			{
				_line.points2.Add(lastPoint);
				_line.points2.Add(nextPoint);
			}
        }
        
        _line.Draw();
	}
Пример #3
0
	public static void GetInternalIntersectionPoint(this Cell cell, MathLine inputLine, Vector2 center, Vector2 direction, float offset, out SideType intersectionSide, out Vector2 intersectionPoint)
	{
		// Exclude Left side from calculations
		var sides = cell.GetCellSides(offset).Where(i => i.Key != SideType.Left);
		// Get best intersection data
		GetIntersectionPoint(center, direction, sides, inputLine, out intersectionSide, out intersectionPoint);
	}