通过三个点绘制一个三角形,三角形中包含已知坐标

时间:2018-11-11 13:13:16

标签: c# algorithm graphics 2d drawing

我正在尝试通过三个点绘制一个三角形,并在三角形内包含已知坐标。
我编写了此算法来完成所有这些工作,但是代码很慢。

有人可以给我另一种简单快速的绘制三角形的方法吗?
我已经掌握了从此站点划清界限的算法,但是对不起,没有提及帖子。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    int Screen_height;
    int Screen_width;

    List<int> Pointsx = new List<int>(new int[] { });
    List<int> Pointsy = new List<int>(new int[] { });

    List<int> edge_one_Tranglex= new List<int>(new int[] { });
    List<int> edge_one_Trangley = new List<int>(new int[] { });

    List<int> edge_two_Tranglex = new List<int>(new int[] { });
    List<int> edge_two_Trangley = new List<int>(new int[] { });

    List<int> edge_three_Tranglex = new List<int>(new int[] { });
    List<int> edge_three_Trangley = new List<int>(new int[] { });

    int edge = 1;
    Bitmap bmp;

    int start = 0;
    int center_x;
    int center_y;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Screen_height = panel1.Height;
        Screen_width = panel1.Width;
        Console.WriteLine(" " + Screen_height + "," + Screen_width);
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (start == 0)
        {
            var sw = new Stopwatch();
            sw.Start();
            bmp = new Bitmap(panel1.Width, panel1.Height);
            panel1.BackgroundImage = (Image)bmp;
            panel1.BackgroundImageLayout = ImageLayout.None;

            //from x to x2 and from y to y2
            //D_line(100, 10, -100, 20);
            D_Triangle(-300, 10, 100, 20, 100, -100);

            sw.Stop();
            Console.WriteLine("" + sw.Elapsed);
            start += 1;
        }
    }

    public void D_line(int x, int y, int x2, int y2)
    {
         center_x = Screen_width / 2;
         center_y = Screen_height / 2;
         line(center_x + x, center_y - y, center_x + x2, center_y - y2);
    }

    public void line(int x, int y, int x2, int y2)
    {
        int w = x2 - x;
        int h = y2 - y;
        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
        if (w < 0) dx1 = -1; else if (w > 0) dx1 = 1;
        if (h < 0) dy1 = -1; else if (h > 0) dy1 = 1;
        if (w < 0) dx2 = -1; else if (w > 0) dx2 = 1;
        int longest = Math.Abs(w);
        int shortest = Math.Abs(h);

        if (!(longest > shortest))
        {
            longest = Math.Abs(h);
            shortest = Math.Abs(w);
            if (h < 0) dy2 = -1; else if (h > 0) dy2 = 1;
            dx2 = 0;
        }

        int numerator = longest >> 1;
        for (int i = 0; i <= longest; i++)
        {
            //putpixel(x, y, color);
            bmp.SetPixel(x, y, Color.Red);
            //my code

            if (edge == 1)
            {
                edge_one_Tranglex.Add(x);
                edge_one_Trangley.Add(y);
            }
            if (edge == 2)
            {
                edge_two_Tranglex.Add(x);
                edge_two_Trangley.Add(y);
            }
            if (edge == 3)
            {
                edge_three_Tranglex.Add(x);
                edge_three_Trangley.Add(y);
            }
            if (edge >= 4)
            {
                if (!Pointsx.Contains(x) || !Pointsy.Contains(y))
                {
                    Pointsx.Add(x);
                    Pointsy.Add(y);
                }
            }

            numerator += shortest;
            if (!(numerator < longest))
            {
                numerator -= longest;
                x += dx1;
                y += dy1;
            }
            else
            {
                x += dx2;
                y += dy2;
            }
        }
        edge++;
        // edge_two_Trangle.ForEach(p => Console.WriteLine(p));
    }

    void D_Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
    {
        D_line(x1, y1, x2, y2);
        D_line(x2, y2, x3, y3);
        D_line(x3, y3, x1, y1);
        int a = edge_two_Tranglex.Count();

        for(int i =1; i < a -1;)
        {
            line(center_x + x1, center_y - y1, edge_two_Tranglex[i], edge_two_Trangley[i]);
            i++;
        }
    }
}

0 个答案:

没有答案