📄 myrectangle.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Painter.Common
{
/// <summary>
/// MyRectAngle 的摘要说明。
/// </summary>
[Serializable]
public class MyRectAngle : MyItem
{
private float x1,y1,x2,y2,x3,y3,x4,y4;
public float X1
{
get
{
return x1;
}
set
{
x1 = value;
}
}
public float Y1
{
get
{
return y1;
}
set
{
y1 = value;
}
}
public float X2
{
get
{
return x2;
}
set
{
x2 = value;
}
}
public float Y2
{
get
{
return y2;
}
set
{
y2 = value;
}
}
public float X3
{
get
{
return x3;
}
set
{
x3 = value;
}
}
public float Y3
{
get
{
return y3;
}
set
{
y3 = value;
}
}
public float X4
{
get
{
return x4;
}
set
{
x4 = value;
}
}
public float Y4
{
get
{
return y4;
}
set
{
y4 = value;
}
}
private double l12=0;
public double L12
{
get
{
if (l12==0)
{
l12 = Math.Sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
return l12;
}
}
private double l13=0;
public double L13
{
get
{
if (l13==0)
{
l13 = Math.Sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
}
return l13;
}
}
private double l14=0;
public double L14
{
get
{
if (l14==0)
{
l14 = Math.Sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
}
return l14;
}
}
public MyRectAngle(float x1,float y1,float x3,float y3,
Color color,float borderwidth,DashStyle style)
{
this.X1 = x1;
this.Y1 = y1;
this.X2 = x3;
this.Y2 = y1;
this.X3 = x3;
this.Y3 = y3;
this.X4 = x1;
this.Y4 = y3;
this.BorderColor = color;
this.BorderWidth = borderwidth;
this.BorderStyle = style;
}
public override void Draw(Graphics g,Color bgColor)
{
Pen pen = new Pen(this.BorderColor,this.BorderWidth);
pen.DashStyle = this.BorderStyle;
g.DrawPolygon(pen,new PointF[]{
new PointF(x1,y1),
new PointF(x2,y2),
new PointF(x3,y3),
new PointF(x4,y4)});
if (IsSelected)
{
pen.Color = SelectedColor;
g.DrawPolygon(pen,new PointF[]{
new PointF(x1,y1),
new PointF(x2,y2),
new PointF(x3,y3),
new PointF(x4,y4)});
}
else if (DrawDeSelect)
{
pen.Color = BorderColor;
g.DrawPolygon(pen,new PointF[]{
new PointF(x1,y1),
new PointF(x2,y2),
new PointF(x3,y3),
new PointF(x4,y4)});
DrawDeSelect = false;
}
}
public override void Erase(Graphics g,Color bgColor)
{
Pen pen = new Pen(bgColor,this.BorderWidth);
pen.DashStyle = this.BorderStyle;
g.DrawPolygon(pen,new PointF[]{
new PointF(x1,y1),
new PointF(x2,y2),
new PointF(x3,y3),
new PointF(x4,y4)});
}
public override void Select(Graphics g,Color bgColor)
{
IsSelected = true;
this.Draw(g,bgColor);
}
public override void DeSelect(Graphics g,Color bgColor)
{
IsSelected = false;
DrawDeSelect = true;
this.Draw(g,bgColor);
}
public override bool Contains(float x, float y)
{
if (LineContains(X1,Y1,X2,Y2,x,y,this.BorderWidth)) {
return true;
}else if (LineContains(X2,Y2,X3,Y3,x,y,this.BorderWidth)) {
return true;
}else if (LineContains(X3,Y3,X4,Y4,x,y,this.BorderWidth)) {
return true;
}else if (LineContains(X4,Y4,X1,Y1,x,y,this.BorderWidth)) {
return true;
}
return false;
}
public override void Rotate(Graphics g, float cx, float cy ,Color bgColor)
{
this.Erase(g,bgColor);
PointF pt3;
pt3 = RotateLine(X1,Y1,cx,cy,L13);
X3 = pt3.X;
Y3 = pt3.Y;
double angle34,angle23;
angle23 = Math.Acos(L12/L13);
angle34 = Math.Acos(L14/L13);
double angle13 = Math.Atan((Y3-Y1)/(X3-X1));
// 如果角度在-PI/2~PI/2之间
if (X3>X1)
{
X2 = X1+(float)(L12*Math.Cos(angle13+angle23));
Y2 = Y1+(float)(L12*Math.Sin(angle13+angle23));
X4 = X1+(float)(L14*Math.Cos(angle13-angle34));
Y4 = Y1+(float)(L14*Math.Sin(angle13-angle34));
}
else
{
X2 = X1-(float)(L12*Math.Cos(angle13+angle23));
Y2 = Y1-(float)(L12*Math.Sin(angle13+angle23));
X4 = X1-(float)(L14*Math.Cos(angle13-angle34));
Y4 = Y1-(float)(L14*Math.Sin(angle13-angle34));
}
this.Draw(g,bgColor);
}
public override void Move(Graphics g, float ptx_offset, float pty_offset, Color bgColor)
{
this.Erase(g,bgColor);
this.X4 += ptx_offset;
this.Y4 += pty_offset;
this.X3 += ptx_offset;
this.Y3 += pty_offset;
this.X2 += ptx_offset;
this.Y2 += pty_offset;
this.X1 += ptx_offset;
this.Y1 += pty_offset;
this.Draw(g,bgColor);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -