【C++学习笔记】CAD中环的偏移学习

【C++学习笔记】CAD中环的偏移学习

最近一直在研究CAD中偏移命令的实现,奈何自己没有技术,就上网找了一个[1]作为记录。


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
struct Point2D
{
float x;
float y;
Point2D(){};

Point2D(float x, float y)
{this->x=x;this->y=y;}

friend Point2D operator +(Point2D a, Point2D b)
{Point2D p;p.x=a.x+b.x;p.y=a.y+b.y;return p;}
friend Point2D operator -(Point2D a, Point2D b)
{Point2D p;p.x=a.x-b.x;p.y=a.y-b.y;return p;}
friend float operator *(Point2D a, Point2D b)
{float p=a.x*b.x+a.y*b.y;return p;}
friend Point2D operator *(Point2D a, float value)
{Point2D p;p.x=a.x*value;p.y=a.y*value;return p;}
friend float xlJi(Point2D a, Point2D b)
{float p=((a.x)*(b.y))-((a.y)*(b.x));return p;}
};

std::ostream& operator <<(std::ostream &s, const Point2D &p)
{
  s << p.x << "," << p.y ;
  return s;
}

int main()
{
    using std::cout;
    using std::endl;
    std::ofstream write;
    write.open("D:\\MY C++ STUDY\\Point data.txt");
std::vector<Point2D> pList;
std::vector<Point2D> DpList;
std::vector<Point2D> nDpList;
std::vector<Point2D> newList;
Point2D p2d,p2;
p2d.x=0;p2d.y=0;pList.push_back(p2d);
p2d.x=100;p2d.y=0;pList.push_back(p2d);
p2d.x=50;p2d.y=50;pList.push_back(p2d);
p2d.x=100;p2d.y=100;pList.push_back(p2d);
p2d.x=0;p2d.y=100;pList.push_back(p2d);
int i,index,count;
count=pList.size();
for(i=0;i<count;i++)
{
index=(i+1)%count;
p2d=pList[index]-pList[i];
DpList.push_back(p2d);
}
// 初始化ndpList,单位化两顶点向量差
float r;
for(i=0;i<count;i++)
{
r=sqrt(DpList[i]*DpList[i]);
r=1/r;
p2d=DpList[i]*r;
nDpList.push_back(p2d);
}
// 计算新顶点, 注意参数dist为负是向内收缩, 为正是向外扩张
//上述说法只是对于顺时针而言
//我把数据改为逆时针,结论刚好相反
float lenth;
float dist=5;
int startindex,endindex;
for(i=0;i<count;i++)
{
startindex= i==0?count-1:i-1;
endindex=i;
float sina=xlJi(nDpList[startindex],nDpList[endindex]);
lenth=dist/sina;
p2d=nDpList[endindex]-nDpList[startindex];
p2=pList[i]+p2d*lenth;
newList.push_back(p2);
cout<<p2<<endl;
write<<p2<<endl;
}
return 0;
}




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值