Documente Academic
Documente Profesional
Documente Cultură
www.cniv.ro
www.icvl.eu
©
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Bresenham
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Bresenham
{
public partial class Form1 : Form
{
private bool draw = false, pause =false;
public Form1()
{
InitializeComponent();
panel1.Paint += new PaintEventHandler(panel1_Paint);
}
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
//intializare grila
for (int i = 1; i < 50; i++)
{
grfx.DrawLine(Pens.LightGray, new Point(0, 10 * i), new
Point(500, 10 * i));
grfx.DrawLine(Pens.LightGray, new Point(10 * i, 0), new
Point(10 * i, 500));
}
if (rbLine.Checked)
{
//puncte pentru linie
int x1, x2, y1, y2;
//puncte initiale, folosite la desenarea liniei ideale
int x1i, x2i, y1i, y2i;
try
{
//extragere coordonate
//sunt dublate in xxi pentru a putea trasa dreapta
ideala
x1i = x1 = Int32.Parse(txtX1.Text);
y1i = y1 = Int32.Parse(txtY1.Text);
x2i = x2 = Int32.Parse(textBox3.Text);
y2i = y2 = Int32.Parse(textBox4.Text);
}
catch (FormatException fe)
{
draw = false;
MessageBox.Show("Eroare la parsarea parametrilor",
"Eroare",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//panta
bool steep = (Math.Abs(y2 - y1) > Math.Abs(x2 - x1));
int aux;
if (steep)
{
aux = x1; x1 = y1; y1 = aux;
aux = x2; x2 = y2; y2 = aux;
}
int dx = x2 - x1;
int dy = Math.Abs(y2 - y1);
int error = 0;
int ystep;
int y = y1;
if (pause)
Thread.Sleep(500);
//linia ideala
if (cbIdeal.Checked)
{
Pen p = new Pen(Color.Red, 3);
grfx.DrawLine(p, x1i * 10 + 5, y1i * 10 + 5, x2i *
10 + 5, y2i * 10 + 5);
}
}
if (rbCircle.Checked)
{
//coordonate centru, raza
int x1, y1, r;
try
{
x1 = Int32.Parse(txtX1.Text);
y1 = Int32.Parse(txtY1.Text);
r = Int32.Parse(textBox3.Text);
}
catch (FormatException fe)
{
draw = false;
MessageBox.Show("Eroare la parsarea parametrilor",
"Eroare",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
}
if (r < 0)
{
draw = false;
MessageBox.Show("Raza nu poate fi negativa",
"Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int f = 1 - r;
int ddF_x = 0;
int ddF_y = -2 * r;
int x = 0;
int y = r;
//marcam centrul
//grfx.FillRectangle(Brushes.Black, x1 * 10, y1 * 10,
10, 10);
while (x < y)
{
if (f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x + 1;
//punctatarea celor 8 puncte pentru x,y,
//cate unul in fiecare octan
if (x1 + x >= 0 && x1 + x < 50 && y1 + y >= 0 && y1
+ y < 50) grfx.FillRectangle(Brushes.Black, (x1 + x)*10, (y1 +
y)*10,10,10);
if (x1 - x >= 0 && x1 - x < 50 && y1 + y >= 0 && y1
+ y < 50) grfx.FillRectangle(Brushes.Black, (x1 - x)*10, (y1 +
y)*10,10,10);
if (x1 + x >= 0 && x1 + x < 50 && y1 - y >= 0 && y1
- y < 50) grfx.FillRectangle(Brushes.Black, (x1 + x)*10, (y1 -
y)*10,10,10);
if (x1 - x >= 0 && x1 - x < 50 && y1 - y >= 0 && y1
- y < 50) grfx.FillRectangle(Brushes.Black, (x1 - x)*10, (y1 -
y)*10,10,10);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
if (x1 + y >= 0 && x1 + y < 50 && y1 + x >= 0 && y1
+ x < 50) grfx.FillRectangle(Brushes.Black, (x1 + y)*10, (y1 +
x)*10,10,10);
if (x1 - y >= 0 && x1 - y < 50 && y1 + x >= 0 && y1
+ x < 50) grfx.FillRectangle(Brushes.Black, (x1 - y)*10, (y1 +
x)*10,10,10);
if (x1 + y >= 0 && x1 + y < 50 && y1 - x >= 0 && y1
- x < 50) grfx.FillRectangle(Brushes.Black, (x1 + y)*10, (y1 -
x)*10,10,10);
if (x1 - y >= 0 && x1 - y < 50 && y1 - x >= 0 && y1
- x < 50) grfx.FillRectangle(Brushes.Black, (x1 - y) * 10, (y1 - x) *
10, 10, 10);
if (pause)
Thread.Sleep(500);
}
//cerc ideal
if (cbIdeal.Checked)
{
Pen p = new Pen(Color.Red, 2);
grfx.DrawEllipse(p, (x1 - r) * 10 + 5, (y1 - r) *
10 + 5, r * 20, r * 20);
}
}
if (rbEllipse.Checked)
{
//date initiale
int x1, y1, r, R;
try
{
x1 = Int32.Parse(txtX1.Text);
y1 = Int32.Parse(txtY1.Text);
r = Int32.Parse(textBox3.Text);
R = Int32.Parse(textBox4.Text);
}
catch (FormatException fe)
{
draw = false;
MessageBox.Show("Eroare la parsarea parametrilor",
"Eroare",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (r < 0 || R < 0)
{
draw = false;
MessageBox.Show("Raza nu poate fi negativa",
"Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
y++;
stopY += dAPatrat;
error += yChange;
yChange += dAPatrat;
if (2*error + xChange >0)
{
x--;
stopX -= dBPatrat;
error += xChange;
xChange += dBPatrat;
}
if (pause)
Thread.Sleep(300);
}
x = 0;
y = R;
xChange = R * R;
yChange = r * r * (1 - 2 * R);
error = 0;
stopX = 0;
stopY = dAPatrat * R;
x++;
stopX += dBPatrat;
error += xChange;
xChange += dBPatrat;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
if (2*error + y > 0)
{
y--;
stopY -= dAPatrat;
error += yChange;
yChange += dAPatrat;
}
if (pause)
Thread.Sleep(300);
}
label3.Text = "X2";
label4.Text = "Y2";
label4.Visible = true;
textBox4.Visible = true;
//curata campurile si zona de desen
btnReset_Click(null, null);
}
label4.Visible = false;
textBox4.Visible = false;
//curata campurile si zona de desen
btnReset_Click(null, null);
}
label4.Visible = true;
textBox4.Visible = true;
//curata campurile si zona de desen
btnReset_Click(null, null);
}
Program.cs
using System;
using
System.Collections.Generic;
using System.Windows.Forms;
namespace Bersenham
{
static class Program
{
/// <summary>
/// The main entry
point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles(
);
Application.SetCompatibleTextRe
nderingDefault(false);
Application.Run(new
FormBresenham());
}
}
}
FormBresenham.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Bersenham
{
/// <summary>
/// Clasa pentru a desena linie, cerc si elipsa cu algoritmul lui
Bresenham
/// </summary>
public partial class FormBresenham : Form
{
private int type;//0 linie, 1 cerc 2 elipsa
private bool click;
private int xprim = -1, yprim = -1;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
private int xsec = -1, ysec = -1;
/// <summary>
/// Constructorul principal
/// </summary>
public FormBresenham()//done
{
InitializeComponent();
type = 0;
click = false;
xprim = yprim = -1;
xsec = ysec = -1;
}
/// <summary>
/// Schimba textul ferestrei si tipul de desen la schimbarea
tab-ului
/// </summary>
/// <param name="sender">Obiectul care a facut apelul</param>
/// <param name="e">Eveniment de tip EventArgs</param>
private void TabControl_SelectedIndexChanged(object sender,
EventArgs e)//done
{
type = TabControl.SelectedIndex;
PanelDraw.Refresh();
}
/// <summary>
/// La apasarea mouse-ului cu butonul STANGA se deseneaza linia
/// </summary>
/// <param name="sender">Obiectul care a facut apelul</param>
/// <param name="e">Eveniment de tip MouseEventArgs</param>
private void PanelDraw_MouseClick(object sender, MouseEventArgs
e)
{
if (e.Button != MouseButtons.Left)
return;
if (click == false)//apasat prima oara
{
click = true;
PanelDraw.Refresh();
public void LinePos1(int x_1, int y_1, int x_2, int y_2)
{
int dx, dy, p, twody, twodydx, x, y, xend;
dx = Math.Abs(x_1 - x_2);
dy = Math.Abs(y_1 - y_2);
twody = 2 * dy;
p = twody - dx;
twodydx = 2 * (dy - dx);
public void LinePos2(int x_1, int y_1, int x_2, int y_2)
{
int dx, dy, p, twodx, twodxdy, x, y, yend;
dx = Math.Abs(x_1 - x_2);
dy = Math.Abs(y_1 - y_2);
twodx = 2 * dx;
p = twodx - dy;
twodxdy = 2 * (dx - dy);
y = y_1; // Dummy initialization
yend = y_2; // Ditto
x = x_1;
public void LineNeg1(int x_1, int y_1, int x_2, int y_2)
{
int dx, dy, p, twody, twodydx, x, y, xend;
dx = Math.Abs(x_2 - x_1);
dy = Math.Abs(y_2 - y_1);
p = 2 * dy - dx;
twody = 2 * dy;
twodydx = 2 * (dy - dx);
public void LineNeg2(int x_1, int y_1, int x_2, int y_2)
{
int dx, dy, p, twodx, twodxdy, x, y, yend;
dx = Math.Abs(x_2 - x_1);
dy = Math.Abs(y_2 - y_1);
p = 2 * dx - dy;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
twodx = 2 * dx;
twodxdy = 2 * (dx - dy);
public void linie(int x1, int y1, int x2, int y2)
{
double deltax, deltay; // For Bresenham's
double slope; // Ditto
else
{
slope = deltay / deltax;
private void elipsa(int x1, int y1, int x2, int y2)
{
int cx, cy;
int a, b;
int x, y;
double d1, d2;
cx = Math.Abs(x1 + x2) / 2;
cy = Math.Abs(y1 + y2) / 2;
a = Math.Abs(x2 - x1) / 2;
b = Math.Abs(y2 - y1) / 2;
x = 0;
y = b;
d1 = b * b - a * a * b + a * a / 4;
pixel(cx + x, cy + y);
pixel(cx + x, cy - y);
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
while (a * a * (y - .5) > b * b * (x + 1))
{
if (d1 < 0)
{
d1 += b * b * (2 * x + 3);
x++;
}
else
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
x++;
y--;
}
pixel(cx + x, cy + y);
pixel(cx + x, cy - y);
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
}
d2 = b * b * (x + .5) * (x + .5) + a * a * (y - 1) * (y -
1) - a * a * b * b;
while (y > 0)
{
if (d2 < 0)
{
d2 += b * b * (2 * x + 2) + a * a * (-2 * y + 3);
y--;
x++;
}
else
{
d2 += a * a * (-2 * y + 3);
y--;
}
pixel(cx + x, cy + y);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
pixel(cx + x, cy - y);
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
}
}
private void cerc(int x1, int y1, int x2, int y2)
{
int cx, cy;
int r;
int x, y;
double d1, d2;
cx = Math.Abs(x1 + x2) / 2;
cy = Math.Abs(y1 + y2) / 2;
r = Math.Min(Math.Abs(x2 - x1), Math.Abs(y2 - y1)) / 2;
x = 0;
y = r;
d1 = r * r * 5 / 4 - r * r * r;
pixel(cx + x, cy + y);
pixel(cx + x, cy - y);
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
while ((y - .5) >(x + 1))
{
if (d1 < 0)
{
d1 += r * r * (2 * x + 3);
x++;
}
else
{
d1 += r * r * (2 * x - 2 * y + 5);
x++;
y--;
}
pixel(cx + x, cy + y);
pixel(cx + x, cy - y);
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
}
d2 = r * r * ((x + .5) * (x + .5) + (y - 1) * (y - 1)) - r
* r * r * r;
while (y > 0)
{
if (d2 < 0)
{
d2 += r * r * (2 * x - 2 * y + 5);
y--;
x++;
}
else
{
d2 += r * r * (-2 * y + 3);
y--;
}
pixel(cx + x, cy + y);
pixel(cx + x, cy - y);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
pixel(cx - x, cy + y);
pixel(cx - x, cy - y);
}
}
System.Threading.Thread.Sleep(1);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace alg_bresenham
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Application.Run(new Form1());
}
}
}
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace alg_bresenham
{
public partial class Form1 : Form
{
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
public Int32 cbox_opt = 10;
public Form1()
{
InitializeComponent();
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
gbox2.Visible = true;
btn1.Visible = true;
btn2.Visible = true;
tb1.Text = "";
tb2.Text = "";
tb3.Text = "";
tb4.Text = "";
pnlDesen.Invalidate();
pnlDesen.Visible = false;
label6.Visible = false;
switch (cbox.SelectedIndex)
{
case 0:
label4.Text = "X2 =";
label5.Text = "Y2 =";
label5.Visible = true;
tb4.Visible = true;
label7.Visible = false;
tb5.Visible = false;
cbox_opt = 0;
break;
case 1:
label4.Text = "R =";
label5.Visible = false;
tb4.Visible = false;
label7.Visible = false;
tb5.Visible = false;
cbox_opt = 1;
break;
case 2:
label4.Text = " r = ";
label5.Text = " R = ";
tb4.Visible = true;
label5.Visible = true;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
label7.Visible = false;
tb5.Visible = false;
cbox_opt = 2;
break;
/* case 3:
label4.Text = " r = ";
label5.Text = " R = ";
tb4.Visible = true;
label5.Visible = true;
label7.Visible = true;
tb5.Visible = true;
cbox_opt = 3;
break;
*/
}
}
tb1.Text = "";
tb2.Text = "";
tb3.Text = "";
tb4.Text = "";
tb5.Text = "";
}
pnlDesen.Invalidate();
}
int xflag = 1;
if (cbox_opt == 10) return;
else
switch (cbox_opt)
{
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
case 0 : //desenam dreapta
// variabile locale folosite in algoritm
int x1, y1, x2, y2, a, b, dl1, dl2, g, nxt, nyt;
try
{
// se scot coordonatele
x1 = Int32.Parse(tb1.Text);
y1 = Int32.Parse(tb2.Text);
x2 = Int32.Parse(tb3.Text);
y2 = Int32.Parse(tb4.Text);
}
catch (FormatException)
{
//datele introduse nu sunt valide( se face verificarea
datelor)
MessageBox.Show("Invalid number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (a <= b)
{
g = 2 * a - b;
dl1 = 2 * (a - b);
dl2 = 2 * a;
}
else
{
g = 2 * b - a;
dl1 = 2 * (b - a);
dl2 = 2 * b;
}
nxt = x2 < x1 ? -1 : 1;
nyt = y2 < y1 ? -1 : 1;
if (a < b)
{
for (int i = 0; i < b; i++)
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
{
if (g > 0)
{
g += dl1;
x1 += nxt;
y1 += nyt;
}
else
{
g += dl2;
x1 += nxt;
}
//se deseneza pixelul la coordonatele calculate
prin algoritm
grafics.FillRectangle(Brushes.BlueViolet, x1, y1,
3, 3);
}
}
else
{
for (int i = 0; i < a; i++)
{
if (g > 0)
{
g += dl1;
y1 += nyt;
x1 += nxt;
}
else
{
g += dl2;
y1 += nyt;
}
//se deseneza pixelul la coordonatele calculate
prin algoritm
grafics.FillRectangle(Brushes.BlueViolet, x1, y1,
3, 3);
}
}
break;
try
{
//se scot coordonatele si raza
xc = Int32.Parse(tb1.Text);
yc = Int32.Parse(tb2.Text);
R = Int32.Parse(tb3.Text);
}
catch (FormatException)
{
//datele introduse nu sunt valide
//mesaj de eroare
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
MessageBox.Show("Invalid number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
x = -1;
y = R;
p = (5 - R * 4) / 4;
while (x <= y)
{
x++;
if (p < 0)
p = p + 2 * x + 1;
else
{
y--;
p = p + 2 * (x - y) + 1;
}
if (x == 0)
{
// se deseneaza pixeli simetrici la
coordonatele calculate conform algoritmului
grafics.FillRectangle(Brushes.BlueViolet, xc,
yc + y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc,
yc - y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc +
y, yc, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
y, yc, 3, 3);
}
if (x == y)
{
grafics.FillRectangle(Brushes.BlueViolet, xc +
x, yc + y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
x, yc + y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc +
x, yc - y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
x, yc - y, 3, 3);
}
if (x < y)
{
grafics.FillRectangle(Brushes.BlueViolet, xc +
x, yc + y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
x, yc + y, 3, 3);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
grafics.FillRectangle(Brushes.BlueViolet, xc +
x, yc - y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
x, yc - y, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc +
y, yc + x, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
y, yc + x, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc +
y, yc - x, 3, 3);
grafics.FillRectangle(Brushes.BlueViolet, xc -
y, yc - x, 3, 3);
}
}
break;
try
{
//se scot coordonatele centrului si razele
xc = Int32.Parse(tb1.Text);
yc = Int32.Parse(tb2.Text);
r = Int32.Parse(tb3.Text);
R = Int32.Parse(tb4.Text);
}
catch (FormatException)
{
//datele introduse nu sunt valide
//se arunca un mesaj de eroare
MessageBox.Show("Invalid number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
R2 = R * R;
r2 = r * r;
x = 0;
y = r;
s = R2 * (1 - 2 * r) + 2 * r2;
t = r2 - 2 * R2 * (2 * r - 1);
do
{
if (s < 0)
{
s = s + 2 * r2 * (2 * x + 3);
t = t + 4 * r2 * (x + 1);
x++;
}
else
if (t < 0)
{
s = s + 2 * r2 * (2 * x + 3) - 4 * R2 * (y
- 1);
t = t + 4 * r2 * (x + 1) - 2 * R2 * (2 * y
- 3);
x++;
y--;
}
else
{
s = s - 4 * R2 * (y - 1);
t = t - 2 * R2 * (2 * y - 3);
y--;
}
}
while (y != 0);
break;
/* case 3:
pi=3.1415926 ;
iXA=(float)Math.Cos(angle)*A ;
iYA=(float)Math.Sin(angle)*A ;
iXB=Math.Cos(angle+(pi/2))*B ;
iYB=Math.Sin(angle+(pi/2))*B ;
ixa2=iXA*iXA ;
iya2=iYA*iYA ;
ixb2=iXB*iXB ;
iyb2=iYB*iYB ;
ixaya=iXA*iYA ;
ixbyb=iXB*iYB ;
ila2=ixa2+iya2 ;
ila4=ila2*ila2 ;
ilb2=ixb2+iyb2 ;
ilb4=ilb2*ilb2 ;
iA=ixa2*ilb4+ixb2*ila4 ;
iB=ixaya*ilb4+ixbyb*ila4;
iC=iya2*ilb4+iyb2*ila4 ;
iD = ila4 * ilb4;
// start AT (-xA,-yA)
{
iX = -iXA;
iY = -iYA;
iDx = -(iB * iXA + iC * iYA);
iDy = iA * iXA + iB * iYA;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
iY++;
iSigma = iA * iX * iX + 2 * iB * iX * iY + iC * iY
* iY - iD;
if (iSigma < 0)
{
iDx = iDx - iB;
iDy = iDy + iA;
iX--;
}
iY++;
iXp1 = iX + 1;
iSigma = iA * iXp1 * iXp1 + 2 * iB * iXp1 * iY +
iC * iY * iY - iD;
if (iSigma >= 0)
{
iDx = iDx + iB;
iDy = iDy - iA;
iX = (float)iXp1;
}
grafics.FillRectangle(Brushes.BlueViolet, iXC + iX
* xflag, iYC + iY, 3, 3);
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
grafics.FillRectangle(Brushes.BlueViolet, iXC - iX
* xflag, iYC - iY, 3, 3);
iX++;
iSigma = iA * iX * iX + 2 * iB * iX * iY + iC * iY
* iY - iD;
if (iSigma < 0)
{
iDx = iDx + iC;
iDy = iDy - iB;
iY++;
}
if (iSigma >= 0)
{
iDx = iDx - iC;
iDy = iDy + iB;
iY = (float)iYm1;
}
iDx = iDx + iB;
iDy = iDy - iA;
}
break;*/
}
}
}
}
}
Main.cs
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace bezier1
{
class MainClass
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace bezier1
{
public partial class MainForm : Form
{
const int max_points = 10;
const int max_aprox_points = 100;
const int numberOfPoints = 40;
const double dt = 1.0 / (numberOfPoints - 1);
public MainForm()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
listBox1.Items.Clear();
N = listBox1.Items.Count - 1;
PrepareValues();
button1.Enabled = false;
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
current_pos = 0;
stop = false;
timer1.Enabled = true;
}
if (listBox1.Items.Count == 0)
return;
grfx.DrawLines(pen, cp);
grfx.DrawLines(pen, curr);
if (stop)
{
pen.Color = ForeColor;
grfx.DrawLines(pen, cp);
for (i = 0; i < listBox1.Items.Count; i++)
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
grfx.DrawRectangle(pen, cp[i].X - 2, cp[i].Y - 2,
4, 4);
}
pen.Color = Color.Red;
grfx.DrawLines(pen, curve);
}
comb[0] = 1;
Point result;
result = new Point(0, 0);
tpow[0] = _tpow[0] = 1;
for (i = 1; i <= N; i++)
{
tpow[i] = tpow[i - 1] * t;
_tpow[i] = _tpow[i - 1] * (1 - t);
}
return result;
}
Conf. Dr. Marin Vlada, Universitatea din Bucuresti
www.ad-astra.ro/marinvlada
current_pos++;
if (current_pos == numberOfPoints)
{
button1.Enabled = true;
timer1.Enabled = false;
stop = true;
}
Invalidate();
}
}
}