private void DDAToolStripMenuItem_Click(object sender, EventArgs e)
{ if (bt == null)
bt = new Bitmap(PictureBox1.Width,PictureBox1.Height); float xa = 33;
float ya = 44;
float xb = 66;
float yb = 234; float delta_x, delta_y;
float x, y;
int dx, dy, steps, k;
dx = (int)(xb - xa);
dy = (int)(yb - ya);
if (Math.Abs(dx) > Math.Abs(dy))
steps = Math.Abs(dx);
else steps = Math.Abs(dy);
delta_x = (float)dx / (float)steps;
delta_y = (float)dy / (float)steps;
x = xa;
y = ya;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
for (k = 1; k <= steps; k++)
{
x += delta_x;
y += delta_y;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
}
PictureBox1.Image = bt;
}
这是之前的DDA画线,但只能定位坐标来画,我问下怎么样才能用鼠标在PictureBox1上面随便画直线??
已知:MouseDown { pStart.X = e.X;
pStart.Y = e.Y;}
MouseUp{ pEnd.X = e.X;
pEnd.Y = e.Y;}
{ if (bt == null)
bt = new Bitmap(PictureBox1.Width,PictureBox1.Height); float xa = 33;
float ya = 44;
float xb = 66;
float yb = 234; float delta_x, delta_y;
float x, y;
int dx, dy, steps, k;
dx = (int)(xb - xa);
dy = (int)(yb - ya);
if (Math.Abs(dx) > Math.Abs(dy))
steps = Math.Abs(dx);
else steps = Math.Abs(dy);
delta_x = (float)dx / (float)steps;
delta_y = (float)dy / (float)steps;
x = xa;
y = ya;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
for (k = 1; k <= steps; k++)
{
x += delta_x;
y += delta_y;
bt.SetPixel((int)x, (int)y, BtnColor.BackColor);
}
PictureBox1.Image = bt;
}
这是之前的DDA画线,但只能定位坐标来画,我问下怎么样才能用鼠标在PictureBox1上面随便画直线??
已知:MouseDown { pStart.X = e.X;
pStart.Y = e.Y;}
MouseUp{ pEnd.X = e.X;
pEnd.Y = e.Y;}
解决方案 »
- 用c#写bho遇到SHDocVw.WebBrowser的问题
- 请问这个checkBox的点击事件怎么写
- 求助 关于C# 系统服务启动
- vs 的及时窗口
- [高分求算法] 如何计算出集合中最多的重复的数字的重复次数
- 类型“System.Xml.Serialization.IXmlSerializable”在未被引用的程序集中定义。
- c#中使用MSXML2.XMLHTTPClass的错误,求解
- 我的Blog写的文章为什么存不了档了呢?
- 又一个可以读写MP3的Tag 标签内容的类
- id 唯一性问题.急
- DevExpress.XtraTreeList.TreeLis 节点排序
- 窗体一装载能不能就模态化与怎样禁止掉键盘上的某些按键
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(bt);
g.DrawLine(Pens.Black, new Point(pStart.X, pStart.Y ), new Point(pEnd.X, pEnd.Y)); this.pictureBox1.Image = bt;
LZ是要用DDA方法来画线。LZ可以把DDA方法定义成一个函数 Bitmap DDA(Point startPoint, Point endPoint)来实现,startPoint和endPoint分别传入鼠标的两个位置就好了
第一次点击确定起点
第二次点击确定终点,而且在鼠标移动过程中也会动态的显示直线
第三次点击画的直线消失namespace WindowsApplication27
{
public partial class Form1 : Form
{
List<Point> Points = new List<Point>();
Point MovePoint;
public Form1()
{
InitializeComponent();
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (Points.Count == 0)
{
Points.Add(e.Location);
MovePoint = new Point(e.X, e.Y);
}
else if (Points.Count == 1)
Points.Add(e.Location);
else if (Points.Count == 2)
{
Points.RemoveAt(0);
Points.RemoveAt(0);
}
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (Points.Count == 1)
MovePoint = new Point(e.X,e.Y);
this.Refresh();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
if (Points.Count == 1)
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), Points[0], MovePoint);
else if (Points.Count == 2)
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), Points[0], Points[1]);
}
}
}
PictureBox1注册MouseDown和MouseUp事件后,就能得到e.Location,就是需要的坐标值啊
public class Pixel
{
public int _x;
public int _y;
public Pixel()
{
this._x = 0;
this._y = 0;
}
public Pixel(int x,int y)
{
this._x = x;
this._y = y;
}
}
public class Point
{
public float x;
public float y;
}
public Pixel LineDDA(Point p0,Point p1,Pixel precedent)
{
Pixel descendent = new Pixel();
float k=Math.Abs((p1.y-p0.y)/(p1.x-p0.x));
if(k>1||k==1)
{
descendent._x = precedent._x + 1;
descendent._y = precedent._y + 1;
}
else
{
descendent._x = precedent._x + 1;
descendent._y = precedent._y + 1;
}
return descendent;
}
MouseDown { pStart.X = e.X;
pStart.Y = e.Y;}
MouseUp{ pEnd.X = e.X;
pEnd.Y = e.Y;}
之前试过老是不行,大家帮下忙......课程设计来的
大哥这是我的msn:[email protected]
可以加我吗?我有很多东西想请教你