实时控制软件设计 第一次作业 Draw

时间:2024-01-06 22:44:56
#include <iostream>
#include <cstring>
#include <math.h>
#include <Eigen/Dense>
using Eigen::MatrixXd;
using namespace std;
class Point
{
public:
double x;
double y;
void Showpoint(void);
void movexy(MatrixXd p);//平移算子
void rotang(double ang);//旋转算子
};
void Point::Showpoint(){
cout<<'('<<x<<','<<y<<')';
}
void Point::movexy(MatrixXd p){
cout<<x<<','<<y<<endl;
x+=p(,);y+=p(,);
cout<<x<<','<<y<<endl;
}
void Point::rotang(double deg){
cout<<x<<','<<y<<endl;
MatrixXd rot(,);//旋转矩阵
rot(,)=cos(deg);
rot(,)=sin(deg);
rot(,)=-sin(deg);
rot(,)=cos(deg);
x=rot(,)*x+rot(,)*y;
y=rot(,)*x+rot(,)*y;
cout<<x<<','<<y<<endl;
}
//点类定义完成 class Element
{
public:
char name[];//名称
int n;
Point pi[];
void Showelement(void);
}; void Element::Showelement(){
cout<<name;
int i;
for(i=;i<=n;i++)pi[i-].Showpoint();
}
//元素类定义完成 Element Move(Element e,MatrixXd p){
int i;
for(i=;i<=e.n;i++)e.pi[i-].movexy(p);
return e;
}
//元素平移
Element Rotate(Element e,double deg){
int i;
for(i=;i<=e.n;i++)e.pi[i-].rotang(deg);
cout<<"旋转命令已执行"<<endl;
return e;
}
//元素旋转 MatrixXd Getpoint(char str[]){
MatrixXd p(,);
int u;
int i,j;
char a[];
for(i=;i<=&&str[i]!='(';i++);
for(j=;j<=&&str[j]!=',';j++);
for(u=;u<=&&i!=j-;u++)
{
i++;
a[u]=str[i];
} p(,)=atof(a);
for(i=j;i<=&&str[i]!=')';i++);
for(u=;u<=&&j!=i-;u++)
{
j++;
a[u]=str[j];
}
p(,)=atof(a);
return p;
}
//从字符串获取点 int main()
{
MatrixXd p1(,),p2(,),p3(,),p4(,);
//用于存放输入点以及位移向量
char str1[],str2[],str3[],str4[],str5[],str6[];
double ang;
Point P1,P2,P3,P4; Element e1;
cout<<"请输入元素(名称、点数、点坐标):";
cin>> e1.name;
cin>>e1.n;
switch(e1.n){
case :
cin>>str1;p1=Getpoint(str1);e1.pi[].x=p1(,), e1.pi[].y=p1(,);
break;
case :
cin>>str1;p1=Getpoint(str1);e1.pi[].x=p1(,), e1.pi[].y=p1(,);
cin>>str2;p2=Getpoint(str2);e1.pi[].x=p2(,), e1.pi[].y=p2(,);
break;
case :
cin>>str1;p1=Getpoint(str1);e1.pi[].x=p1(,), e1.pi[].y=p1(,);
cin>>str2;p2=Getpoint(str2);e1.pi[].x=p2(,), e1.pi[].y=p2(,);
cin>>str3;p3=Getpoint(str3);e1.pi[].x=p3(,), e1.pi[].y=p3(,);
break;
default:cout<<"输入错误";
}
//根据点数获取内容并构造Element对象 cout<<"请对当前输入元素输入操作命令:";
cin>>str4;cin>>str5;cin>>str6;
if(str4[]=='m'&&str4[]=='o'&&str4[]=='v'&&str4[]=='e'){
cout<<"命令为move"<<endl;
p4=Getpoint(str6);
Element e2;e2=Move(e1,p4);
e2.Showelement();
cout<<endl;
}
if(str4[]=='r'&&str4[]=='o'&&str4[]=='t'&&str4[]=='a'&&str4[]=='t'&&str4[]=='e'&&str4[]==){
ang=atof(str6)*3.1415926/;
cout<<"命令为rotate"<<endl;
Element e2;e2=Rotate(e1,ang);
e2.Showelement();
cout<<endl;
} }