Draw a Triangle through three points, with known coordinates enclosed within the Triangle









up vote
0
down vote

favorite
1












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)
!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++;












share|improve this question























  • 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 at 13:45










  • Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
    – Ali jamal
    Nov 11 at 14:37










  • See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
    – Jimi
    Nov 11 at 14:45











  • Points inside a triangle
    – John
    Nov 11 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 at 15:09














up vote
0
down vote

favorite
1












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)
!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++;












share|improve this question























  • 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 at 13:45










  • Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
    – Ali jamal
    Nov 11 at 14:37










  • See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
    – Jimi
    Nov 11 at 14:45











  • Points inside a triangle
    – John
    Nov 11 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 at 15:09












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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)
!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++;












share|improve this question















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)
!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++;









c# algorithm graphics 2d drawing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 14:58









Jimi

6,07421233




6,07421233










asked Nov 11 at 13:13









Ali jamal

114




114











  • 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 at 13:45










  • Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
    – Ali jamal
    Nov 11 at 14:37










  • See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
    – Jimi
    Nov 11 at 14:45











  • Points inside a triangle
    – John
    Nov 11 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 at 15:09
















  • 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 at 13:45










  • Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
    – Ali jamal
    Nov 11 at 14:37










  • See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
    – Jimi
    Nov 11 at 14:45











  • Points inside a triangle
    – John
    Nov 11 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 at 15:09















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 at 13:45




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 at 13:45












Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
– Ali jamal
Nov 11 at 14:37




Ok .if I use Graphics.DrawPolygon. how to get the all points inside the polygon ?
– Ali jamal
Nov 11 at 14:37












See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
– Jimi
Nov 11 at 14:45





See the example in the Docs about Graphics.DrawPolygon. Use PointF structures and, possibly, all floating point values (float) instead of integers to perform the calculations.
– Jimi
Nov 11 at 14:45













Points inside a triangle
– John
Nov 11 at 14:47




Points inside a triangle
– John
Nov 11 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 at 15:09




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 at 15:09

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249085%2fdraw-a-triangle-through-three-points-with-known-coordinates-enclosed-within-the%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249085%2fdraw-a-triangle-through-three-points-with-known-coordinates-enclosed-within-the%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto