在C#中如计算两点之间的距离和相对点
本帖最后由 作者 于 2010-11-2 22:02:34 编辑 <br /><br /> <p><font face="Verdana">请教在<font color="#0000ff">C#</font>中如计算两点之间的距离和相对点来完成以下图示的图形!</font></p><p> </p> <p>自己先顶一下。。。</p> <p><font face="Verdana"><a href="http://bbs.mjtd.com/forum.php?mod=viewthread&tid=81457">http://bbs.mjtd.com/forum.php?mod=viewthread&tid=81457</a></font></p>
<p> </p> 多谢孤哥的指点,我先去看看,然后研究一下... 狐哥,我刚结束C#,还是看不懂你所说的那个向量如何用,你能否帮我写个简单的事例阿,我想这样我就可以通过在调试的过程中去理解
Point3d pt1 = Point3d.Origin, pt2 = new Point3d(10, 5, 0);
//距离
double dist1 = pt2.DistanceTo(pt1);
double dist2 = (pt2 - pt1).Length;
double ang = Math.Atan(1) / 45 * 30;
double dist = 10;
//极轴
Point3d pt3 = pt1 + Vector3d.XAxis.RotateBy(ang, Vector3d.ZAxis) * dist;
<p>狐哥,</p>
<p> 我按你提供的例子试着写了一下,可以绘出图形,但是我还是不知如何做到按任意两点来绘图,</p>
<p>我实际上是想达到以下效果,请看以下图片中效果,请再帮我按以下动画写个简单的例子,先谢了。</p>
<p> </p> <p>先把你的代码贴上吧?</p>
<p>看看能不能帮你简单改下</p> //CAD命令行名称
public void YTest()
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions proOne = new PromptPointOptions("\n请选取第一个点:");
//定义第一个点
Point3d ptOne = new Point3d();
//定义第二个点
Point3d ptTwo = new Point3d();
PromptPointResult resOne = ed.GetPoint(proOne);
if (resOne.Status == PromptStatus.OK)
{
ptOne = resOne.Value;
PromptPointOptions proTwo = new PromptPointOptions("\n请选取第二个点:");
proTwo.BasePoint = ptOne;
proTwo.UseBasePoint = true;
PromptPointResult resTwo = ed.GetPoint(proTwo);
if (resTwo.Status == PromptStatus.OK)
{
ptTwo = resTwo.Value;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite, false);
Line lineObject = new Line(ptOne, ptTwo);
double douLineAngle = lineObject.Angle;
#region
//----------------------------------------------------------------------------------------------
//double douDis = 0.5;
Point3d ptLineBen1 = new Point3d(ptOne.X + douDis / 2 * Math.Cos(douLineAngle + Math.PI / 2),
ptOne.Y + douDis / 2 * Math.Sin(douLineAngle + Math.PI / 2),
ptOne.Z);
Point3d ptLineEnd1 = new Point3d(ptTwo.X + douDis / 2 * Math.Cos(douLineAngle + Math.PI / 2),
ptTwo.Y + douDis / 2 * Math.Sin(douLineAngle + Math.PI / 2),
ptTwo.Z);
Point3d ptLineBen2 = new Point3d(ptOne.X + douDis / 2 * Math.Cos(douLineAngle - Math.PI / 2),
ptOne.Y + douDis / 2 * Math.Sin(douLineAngle - Math.PI / 2),
ptOne.Z);
Point3d ptLineEnd2 = new Point3d(ptTwo.X + douDis / 2 * Math.Cos(douLineAngle - Math.PI / 2),
ptTwo.Y + douDis / 2 * Math.Sin(douLineAngle - Math.PI / 2),
ptTwo.Z);
//---这是你提供的代码
Point3d pt1 = Point3d.Origin, pt2 = new Point3d.ptOne;
//距离
double dist1 = pt2.DistanceTo(pt1);
double dist2 = (pt2 - pt1).Length;
//double ang = Math.Atan(1) / 45 * 30;
double dist = 10;
//极轴
Point3d pt3 = pt1 + Vector3d.XAxis.RotateBy(douLineAngle, Vector3d.XAxis) * dist;
//---这是你提供的代码
//----------------------------------------------------------------------------------------------
#endregion
Line lineObject1 = new Line(ptLineBen1, ptLineEnd1);
Line lineObject2 = new Line(ptLineBen2, ptLineEnd2);
Line lineObject3 = new Line(ptLineBen2, pt3);
btr.AppendEntity(lineObject1);
btr.AppendEntity(lineObject2);
btr.AppendEntity(lineObject3);
trans.AddNewlyCreatedDBObject(lineObject1, true);
trans.AddNewlyCreatedDBObject(lineObject2, true);
trans.AddNewlyCreatedDBObject(lineObject3, true);
lineObject.Dispose();
trans.Commit();
}
}
}
}
} 重写了一次,还没完,后面的自己试着写下
public void test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (doc.LockDocument())
{
PromptPointOptions optPt1 = new PromptPointOptions("\n请选取第一个点:");
PromptPointResult resPt1 = ed.GetPoint(optPt1);
if (resPt1.Status != PromptStatus.OK)
return;
Point3d pt1 = resPt1.Value;
PromptPointOptions optPt2 = new PromptPointOptions("\n请选取第二个点:");
optPt2.BasePoint = pt1;
optPt2.UseBasePoint = true;
PromptPointResult resPt2 = ed.GetPoint(optPt2);
if (resPt2.Status != PromptStatus.OK)
return;
Point3d pt2 = resPt2.Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite, false);
//pt1->pt2方向的单位矢量
Vector3d vec1 = (pt2 - pt1).GetNormal();
//垂直方向的向量,长度为5
Vector3d vec2 = 5 * vec1.RotateBy(Math.PI / 2, Vector3d.ZAxis);
List<Line> lines = new List<Line>();
lines.Add(new Line(pt1 - 2 * vec1, pt2 + 2 * vec1));
lines.Add(new Line(pt1 + vec2, pt2 + vec2));
lines.Add(new Line(pt1 - vec2, pt2 - vec2));
foreach (Line line in lines)
{
btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
tr.Commit();
}
}
}
页:
[1]
2