0

I'am trying to Draw a Triangle in through three points, with known coordinates enclosed within the Triangle.
I wrote this algorithm to do all this, but the code is slow.

Can anyone give me another easy and faster way to draw a Triangle?
I have got the algorithm of drawing the line from this site but do not mention the post sorry.

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++;
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
Ali jamal
  • 31
  • 6
  • Huh? what are you really trying to achieve?? To draw you would hardly ever use Bitmap.SetPixel but always Graphics.DrawPolygon. Google it! – TaW Nov 11 '18 at 13:45
  • Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ? – Ali jamal Nov 11 '18 at 14:37
  • See the example in the Docs about [Graphics.DrawPolygon](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawpolygon?view=netframework-4.7.2). Use [PointF](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.pointf?view=netframework-4.7.2) structures and, possibly, all floating point values (`float`) instead of integers to perform the calculations. – Jimi Nov 11 '18 at 14:45
  • [Points inside a *triangle*](https://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle) – ProgrammingLlama Nov 11 '18 at 14:47
  • Well, I understand you now. I'm actually trying to write a graphics library so I can not use the graphics library because then I will convert the codes to other languages ​​so it did not work with me. All that benefits me now is if you know a way faster than my way it will be better – Ali jamal Nov 11 '18 at 15:09
  • _how to get the all points inside the polygon_ You add the polygon to a GraphicsPath and can then test with [`GraphicsPath.IsVisible`](https://learn.microsoft.com/de-de/dotnet/api/system.drawing.drawing2d.graphicspath.isvisible?view=netframework-4.7.2) – TaW Nov 11 '18 at 19:02
  • Sir .just add panel1 to Windows form and copy past my code the code is run ‍♀️ but my algorithm is slow .take 0.1 second to draw the triangle from scratch and add every point in triangle to list .can you help me with another fast algorithm for draw triangle from scratch – Ali jamal Nov 11 '18 at 20:57
  • Of course it is slow. I already told you how to draw a polygon. Throw the code away and start anew.. `Graphics.DrawPolygon(somePen, somePointList.ToArray());` – TaW Nov 11 '18 at 21:13
  • Yes but I need to done this without using Graphics.DrawPolygon – Ali jamal Nov 11 '18 at 21:53
  • What other languages? All high level languages have their own dedicated functions to draw a Polygon and most of these languages use very similar notations for these tasks. You do understand that, as of now, you are already referencing a `System.Drawing.Bitmap` class using its `.SetPixel()` method to draw the line. How does this comply with other languages classes, specific objects and methods? Java has its `awt.Polygon` class, Python its `matplotlib.pyplot`. The underlying logic is the same. You'll have much less work when converting your code to other languages if you follow the common path. – Jimi Nov 12 '18 at 08:54
  • Well, now things are clear to me. You helped me a lot with this information. Thank you very much.please add your answer in post – Ali jamal Nov 12 '18 at 10:45
  • If, by chance, you're asking me, I can't do that. You question is *Draw a Triangle through three points (...)*. None of my comments answer this question. Btw, remember to prefix the nickname of the person you want to talk to with a `@` symbol, otherwise nobody is notified of your comments. – Jimi Nov 12 '18 at 14:56
  • ok ...Well thank you anyway @Jimi – Ali jamal Nov 12 '18 at 15:56

0 Answers0