using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestPingxingZhexian
{
public partial class Form1: Form
{
struct LineF
{
public PointF P1;
public PointF P2;
public LineF( PointF p1, PointF p2 )
{
P1 = p1;
P2 = p2;
}
}
enum Direction
{
Left,
Right
}
public Form1()
{
InitializeComponent();
}
Pen penRing = Pens.Gray;
Pen penLine = Pens.LightPink;
int scale = 20;
/// <summary>
/// 原点显示坐标
/// </summary>
Point pt0;
Graphics g;
void DrawingInit()
{
Image img = new Bitmap( pictureBox1.Width, pictureBox1.Height );
this.pictureBox1.Image = img;
g = Graphics.FromImage( img );
g.Clear( Color.Black );
pt0 = new Point( pictureBox1.Width / 2, pictureBox1.Height / 2 );
// X, Y 轴线
g.DrawLine( penRing, pt0.X, pt0.Y - pictureBox1.Height / 2 + 10, pt0.X, pt0.Y + pictureBox1.Height / 2 - 10 );
g.DrawLine( penRing, pt0.X - pictureBox1.Width / 2 + 10, pt0.Y, pt0.X + pictureBox1.Width / 2 - 10, pt0.Y );
// 原点
g.DrawRectangle( penRing, pt0.X - 1, pt0.Y - 1, 2, 2 );
}
void DrawLines( Pen pen, PointF[] points )
{
PointF a = points[0];
for ( int i = 1; i < points.Length; i++ )
{
PointF b = points[i];
g.DrawLine( pen, PointF.Add( pt0, new SizeF( a.X * scale, -a.Y * scale ) ), PointF.Add( pt0, new SizeF( b.X * scale, -b.Y * scale ) ) );
a = b;
}
}
double GetLineAngle( PointF a, PointF b )
{
return Math.Atan2( a.Y - b.Y, a.X - b.X );
}
/// <summary>
/// 得到偏移点
/// </summary>
/// <param name="point">基准点</param>
/// <param name="angle">偏移弧度</param>
/// <param name="distance">偏移距离</param>
/// <returns></returns>
PointF GetPointOffset( PointF point, double angle, float distance )
{
PointF tar = new PointF();
float dX = (float)(Math.Cos( angle ) * distance);
float dY = (float)(Math.Sin( angle ) * distance);
tar.X = point.X - dX;
tar.Y = point.Y - dY;
return tar;
}
PointF GetIntersectPoint( PointF p1, PointF p2, PointF p3, PointF p4 )
{
float d = (p2.Y - p1.Y) * (p4.X - p3.X) - (p4.Y - p3.Y) * (p2.X - p1.X);
if ( d == 0 )
{
throw new ArithmeticException("两条直线平行,没有交点");
}
PointF p = new PointF();
p.X = ((p2.X - p1.X) * (p4.X - p3.X) * (p3.Y - p1.Y) + (p2.Y - p1.Y) * (p4.X - p3.X) * p1.X - (p4.Y - p3.Y) * (p2.X - p1.X) * p3.X) / d;
p.Y = ((p2.Y - p1.Y) * (p4.Y - p3.Y) * (p3.X - p1.X) + (p2.X - p1.X) * (p4.Y - p3.Y) * p1.Y - (p4.X - p3.X) * (p2.Y - p1.Y) * p3.Y ) / (-d);
return p;
}
/// <summary>
/// 取得交点坐标
/// </summary>
/// <param name="L1">直线1</param>
/// <param name="L2">直线2</param>
/// <returns></returns>
PointF GetIntersectPoint( LineF L1, LineF L2 )
{
return GetIntersectPoint( L1.P1, L1.P2, L2.P1, L2.P2);
}
/// <summary>
/// 得到平行的折线
/// </summary>
/// <param name="points">基准折线的点集合</param>
/// <param name="distance">距离</param>
/// <param name="dir">方向(左或右)</param>
/// <returns></returns>
PointF[] GetParallelLines( PointF[] points, float distance, Direction dir )
{
List<PointF> tarPoints = new List<PointF>();
PointF a = points[0];
LineF? lineLast = null;
for ( int i = 1; i < points.Length; i++ )
{
PointF b = points[i];
double angle = GetLineAngle( a, b );
if ( dir == Direction.Left )
{
angle += Math.PI / 2;
}
else
{
angle -= Math.PI / 2;
}
PointF p1 = GetPointOffset( a, angle, distance );
PointF p2 = GetPointOffset( b, angle, distance );
if ( lineLast == null )
{
tarPoints.Add( p1 );
lineLast = new LineF( p1, p2 );
}
else
{
LineF lineCurrent = new LineF( p1, p2 );
PointF px = GetIntersectPoint( lineLast.Value, lineCurrent );
tarPoints.Add( px );
lineLast = lineCurrent;
}
a = b;
}
if ( lineLast != null )
{
tarPoints.Add( lineLast.Value.P2 );
}
return tarPoints.ToArray();
}
private void Form1_Load( object sender, EventArgs e )
{
DrawingInit();
// 定义折线
PointF[] points = new PointF[] {
new PointF(-2,-3),
new PointF(-1,1),
new PointF(-4,2),
//new PointF( 1.5f, 5 ),
new PointF( 3, -2) };
for ( int i = 0; i < points.Length; i++ )
{
Console.WriteLine( "{0}, {1}, {2}", points[i].X, points[i].Y, Math.Atan2(points[i].X,points[i].Y) );
}
DrawLines( penLine, points );
PointF[] nps = GetParallelLines( points, 1f, Direction.Left );
DrawLines( Pens.Yellow, nps );
nps = GetParallelLines( points, 1f, Direction.Right );
DrawLines( Pens.Yellow, nps );
}
}
}
Notemper2x 3.1 ( ̄ε( ̄#)
没脾气2x 之 个人综合篇:
http://notemper2x.cndev.org/
我的 panoramio 相册:
http://panoramio.com/user/zhaixudong
我的 flickr相册:
http://www.flickr.com/photos/notemper2x/


QQ号20250出售,售价400,000元整(5位、皇冠80级、VIP7)a