/Rendering/Basics/Drawing/Line.cs

#
C# | 117 lines | 73 code | 11 blank | 33 comment | 3 complexity | b3a756500ba4520436e086a2f96b2fe7 MD5 | raw file
``````
using System;

using Delta.Rendering.Cameras;

using Delta.Utilities.Datatypes;

namespace Delta.Rendering.Basics.Drawing

{

/// <summary>

/// Draw 2D or 3D lines with help of this class. Rarely used. If you want

/// to render more complex shapes use one of the other classes here or

/// implement one yourself. This has the advantage of optimizing it better.

/// </summary>

public static class Line

{

#region Draw (Static)

/// <summary>

/// Draw line in 2D

/// </summary>

/// <param name="startPosition">Start position in quadratic space.</param>

/// <param name="endPosition">End position in quadratic space.</param>

/// <param name="lineColor">Line color</param>

public static void Draw(Point startPosition, Point endPosition,

Color color)

{

#region Culling check

// Skip if the line is certainly outside of the quadratic space.

// Checking the screen area would work too, but is a little slower and

// won't exclude much more anyway.

float smallestX = startPosition.X < endPosition.X

? startPosition.X

: endPosition.X;

float smallestY = startPosition.Y < endPosition.Y

? startPosition.Y

: endPosition.Y;

float distanceX = Math.Abs(startPosition.X - endPosition.X);

float distanceY = Math.Abs(startPosition.Y - endPosition.Y);

if (smallestX >= 1.0f ||

smallestY >= 1.0f ||

smallestX < -distanceX ||

smallestY < -distanceY)

{

// Skip this line rendering, not visible this frame.

WasLastLineCulled = true;

return;

}

WasLastLineCulled = false;

#endregion

DrawManager.Instance.Draw2DLine(ref startPosition, ref endPosition,

ref color);

}

#endregion

#region DrawCross (Static)

/// <summary>

/// Draws a cross in 2D

/// </summary>

/// <param name="position">Position</param>

/// <param name="color">Color</param>

public static void DrawCross(Point position, float radius, Color color)

{

Draw(new Point(position.X - radius, position.Y),

new Point(position.X + radius, position.Y),

color);

Draw(new Point(position.X, position.Y - radius),

new Point(position.X, position.Y + radius),

color);

}

#endregion

#region Draw (Static)

/// <summary>

/// Draw line in 3D

/// </summary>

/// <param name="startPosition">Start position</param>

/// <param name="endPosition">End position</param>

/// <param name="color">Line color</param>

public static void Draw(Vector startPosition, Vector endPosition,

Color color)

{

#region Culling check

Plane[] frustum = BaseCamera.Current.ViewFrustum;

// Check all the frustum planes.

for (byte planeId = 0; planeId < 6; planeId++)

{

// is the positiveVertex outside?

if (frustum[planeId].DotCoordinate(startPosition) < -0.001f &&

frustum[planeId].DotCoordinate(endPosition) < -0.001f)

{

WasLastLineCulled = true;

return;

}

}

WasLastLineCulled = false;

#endregion

DrawManager.Instance.Draw3DLine(ref startPosition, ref endPosition,

ref color);

}

#endregion

#region Internal

#region WasLastLineCulled (Internal)

/// <summary>

/// Helper boolean for the Culling Draw Test which counts the

/// number of drawn lines.

/// </summary>

internal static bool WasLastLineCulled;

#endregion

#endregion

}

}

``````