Hideously over-complicated method for drawing a line between 2 points on a 2d wrap-around map. Must be a much nicer solution for this, probably involving triangles;) - CSharp System

CSharp examples for System:Math Geometry

Description

Hideously over-complicated method for drawing a line between 2 points on a 2d wrap-around map. Must be a much nicer solution for this, probably involving triangles;)

Demo Code


using System.Drawing;
using System;/*from  ww  w. j  a v  a  2 s .  co m*/

public class Main{
        /// <summary>
        /// Hideously over-complicated method for drawing a line between 2 points
        /// on a 2d wrap-around map.  Must be a much nicer solution for this, probably
        /// involving triangles;)
        /// </summary>
        public static void DrawLine(Point a, Point b, int size, Graphics g, Pen pen)
        {
            Point left = new Point(-(size * 2), size);
            Point right = new Point(size * 2, size);
            Point top = new Point(size, -(size * 2));
            Point bottom = new Point(size, size * 2);

            Point intersectX = new Point();
            Point intersectY = new Point();

            if (WrapX(a, b, size) && WrapY(a, b, size)) // cross both axis 
            {
                if (a.X > b.X && a.Y < b.Y) // top-right
                {
                    IntersectionOfTwoLines(a, new Point(b.X + size, b.Y - size), top, bottom, ref intersectY);
                    IntersectionOfTwoLines(new Point(a.X - size, a.Y + size), b, left, right, ref intersectX);
                    g.DrawLine(pen, a, intersectY);
                    g.DrawLine(pen, b, intersectX);
                    g.DrawLine(pen, new Point(intersectX.X, 0), new Point(0, intersectY.Y));
                }
                else if (a.X < b.X && a.Y < b.Y) // top-left [todo]
                {
                    IntersectionOfTwoLines(a, new Point(b.X + size, b.Y + size), left, right, ref intersectY);
                    IntersectionOfTwoLines(a, new Point(b.X + size, b.Y + size), top, bottom, ref intersectX);
                    g.DrawLine(pen, a, intersectY);
                    g.DrawLine(pen, b, intersectX);
                }
                else if (a.X > b.X && a.Y > b.Y) // bottom-right [todo]
                {
                }
                else if (a.X < b.X && a.Y > b.Y)  // bottom-left [todo]
                {
                }
            }
            else if (WrapX(a, b, size)) // cross y-axis 
            {
                if (a.X > b.X) // right 
                {
                    IntersectionOfTwoLines(a, new Point(b.X + size, b.Y), top, bottom, ref intersectY);
                    g.DrawLine(pen, a, intersectY);
                    g.DrawLine(pen, b, new Point(0, intersectY.Y));
                }
                else // left 
                {
                    IntersectionOfTwoLines(new Point(a.X + size, a.Y), b, top, bottom, ref intersectY);
                    g.DrawLine(pen, a, new Point(0, intersectY.Y));
                    g.DrawLine(pen, b, new Point(size, intersectY.Y));
                }
            }
            else if (WrapY(a, b, size)) // cross x-axis 
            {
                if (a.Y < b.Y) // top
                {
                    IntersectionOfTwoLines(new Point(a.X, a.Y + size), b, left, right, ref intersectX);
                    g.DrawLine(pen, a, new Point(intersectX.X, 0));
                    g.DrawLine(pen, b, intersectX);
                }
                else // bottom
                {
                    IntersectionOfTwoLines(a, new Point(b.X, b.Y + size), left, right, ref intersectX);
                    g.DrawLine(pen, a, intersectX);
                    g.DrawLine(pen, b, new Point(intersectX.X, 0));
                }
            }
            else // just draw the ruddy line 
            {
                g.DrawLine(pen, a, b);
            }
        }
}

Related Tutorials