Нужно реализовать алгоритм Сазерленда-Коэна на C#, для пояснения алгоритма вот ссылка
http://www.mari.ru/mmlab/home/kg/Lection9/3.html а вот код, только не работает(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Лаб_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public System.Drawing.Graphics Gr;
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(300, 300);
System.Drawing.Graphics Gr = Graphics.FromImage(bmp);
Gr.Clear(Color.White);
Gr.DrawRectangle(Pens.Gray, 100, 100, 100, 100);
pictureBox1.Image = bmp;
}
void end(int x, int y, ref Int32 kod, ref Int32 summ)
{
kod = 0;
summ = 0;
int xl = 0;
int xr = 0;
int yn = 0;
int yv = 0;
if (x < xl) { kod += 1; summ++; };
if (x > xr) { kod += 2; summ++; };
if (y > yn) { kod += 4; summ++; };
if (y < yv) { kod += 8; summ++; };
}
void koen(Int32 x1, Int32 y1, Int32 x2, Int32 y2, ref Int32 vid, ref Int32 kod1, ref Int32 kod2)
{
Int32 sum1 = 0, sum2 = 0;
end(x1, y1, ref kod1, ref sum1);
end(x2, y2, ref kod2, ref sum2);
vid = 2;
if (sum1 == 0 && sum2 == 0) vid = 1;
else
{
Int32 pr = kod1 & kod2;
if (pr != 0) vid = 0;
}
}
int search(Int32 kod, Int32 i)
{
if (i == 4) return kod & 8;
if (i == 3) return kod & 4;
if (i == 2) return kod & 2;
if (i == 1) return kod & 1; return 0;
}
void main()
{
Int32 x1 = Convert.ToInt32(textBox1.Text);
Int32 y1 = Convert.ToInt32(textBox2.Text);
Int32 x2 = Convert.ToInt32(textBox3.Text);
Int32 y2 = Convert.ToInt32(textBox4.Text);
Int32[] okno = new Int32[5];
okno[1] = 100;
okno[2] = 200;
okno[3] = 200;
okno[4] = 100;
Int32 flag = 1;
Double naklon = 0;
Int32 kod1 = 0, kod2 = 0;
if (x2 - x1 == 0) flag = -1;
else
{
naklon = Convert.ToDouble(y2 - y1) / Convert.ToDouble(x2 - x1);
if (naklon == 0) flag = 0;
}
for (Int32 i = 1; i <= 4; i++)
{
Int32 vid = 0;
koen(x1, y1, x2, y2, ref vid, ref kod1, ref kod2);
if (vid == 1) Gr.DrawLine(Pens.Gray, x1, y1, x2, y2);
if (vid == 0) return;
if (search(kod1, i) == (search(kod2, i))) continue;
if (search(kod1, i) == 0)
{
Int32 temp = x1; x2 = x1; x2 = temp;
temp = y1; y2 = y1; y2 = temp;
}
if ((flag != -1) && (i <= 2))
{
y1 = Convert.ToInt32(naklon * Convert.ToDouble(okno[i] - x1)) + y1;
x1 = okno[i];
}
else
{
x1 = Convert.ToInt32((1 / naklon) * Convert.ToDouble(okno[i] - y1)) + x1;
y1 = okno[i];
}
}
Gr.DrawLine(Pens.Gray, x1, y1, x2, y2);
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(300, 300);
Gr = Graphics.FromImage(bmp);
Gr.Clear(Color.White);
Gr.DrawRectangle(Pens.Gray, 100, 100, 100, 100);
main();
pictureBox1.Image = bmp;
}
}
}