Eigen库实现简单的旋转、平移操作
本来课程要求用GUI界面来实现Eigen的旋转、平移操作的,但是接触GUI编程时间太短,虽然要求很简单,但是做了几天还是没有完成。就把命令行下面的简单的贴一下吧。
main.cpp
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include "func.h"
#include "Point.h"
#include "Shape.h"
enum Motion {
Rotate = 1,
Move,
Zoom,
};
std::map<std::string, Motion> motion = {
{"rotate", Motion::Rotate},
{"move", Motion::Move},
{"zoom", Motion::Zoom},
};
Motion resolveCommand(std::string command) {
return motion[command];
}
int main() {
std::string name, pointNum;
printf("请输入图形的名字:");
getline(std::cin, name);
if (name.empty()) {
name = "T";
printf("你的输入为空,因此为你的图形命名(T)\n");
}
printf("请输入图形的端点数量:");
getline(std::cin, pointNum);
if (is_number(pointNum)) {
std::cout << "你输入的是整数\n";
} else {
std::cout << "你输入的不是整数\n";
}
int num = atoi(pointNum.c_str());
Point *shapePoints = new Point[num];
for (int i = 0; i < num; ++i) {
double x, y;
printf("请输入第%d个点横坐标x = ", i + 1);
scanf("%lf", &x);
printf("请输入第%d个点纵坐标y = ", i + 1);
scanf("%lf", &y);
shapePoints[i].update(x, y);
}
std::cout << "你输入图形的名字为:" << name << " " << "你输入的端点数量为:" << pointNum << std::endl;
Shape *shape = new Shape(shapePoints, num, name.c_str());
shape->printShape();
std::string command;
bool flag = true;
while (flag) {
printf("\n请输入对图形的操作指令:");
std::cin.ignore();
getline(std::cin, command);
transform(command.begin(),command.end(),command.begin(), ::tolower); //大小写转换
switch (resolveCommand(command)) {
case Motion::Rotate:
double angle;
printf("请输入旋转角度,逆时针为正:");
scanf("%lf", &angle);
shape->rotateShape(angle);
break;
case Motion::Move:
double mx, my;
printf("输入X方向移动距离:");
scanf("%lf", &mx);
printf("输入Y方向移动距离:");
scanf("%lf", &my);
shape->moveShape(mx, my);
break;
case Motion::Zoom:
double zx, zy;
printf("输入X方向缩放大小:");
scanf("%lf", &zx);
printf("输入Y方向缩放大小:");
scanf("%lf", &zy);
shape->zoomShape(zx, zy);
break;
default:
flag = false;
}
shape->printShape();
}
printf("Error!!!你输入的命令有误,已经退出指令模式退出\n");
delete shape;
delete[] shapePoints;
shapePoints = NULL;
定义了一个Point点类
point.h
#include "Eigen/Dense"
using namespace Eigen;
class Point {
private:
double x, y;
public:
Point();
Point(double x, double y);
void update(double x, double y);
void print();
void move(double x, double y);
void rotate(double angle);
void zoom(double zx, double zy);
Vector2d pointByMatrix();
void setByMatrix(Vector2d m);
~Point();
};
point.cpp
#include "Point.h"
#include <iostream>
Point::Point() {
this->x = 0;
this->y = 0;
}
Point::Point(double x, double y) {
this->x = x;
this->y = y;
}
void Point::update(double x, double y) {
this->x = x;
this->y = y;
}
void Point::print() {
std::cout << "(" << this->x << ", " << this->y << ")";
}
Point::~Point() {
std::cout << "调用Point析构函数" << std::endl;
}
/**
* 平移
*/
void Point::move(double x, double y) {
Vector2d a = pointByMatrix();
Vector2d b(x, y);
a = a + b;
setByMatrix(a);
}
/**
* 旋转变换,angle为角度, 逆时针为正, 顺时针为负
*/
void Point::rotate(double angle) {
MatrixXd T(2, 2);
angle = angle / 180 * M_PI;
T(0, 0) = cos(angle);
T(0, 1) = sin(angle);
T(1, 0) = -sin(angle);
T(1, 1) = cos(angle);
std::cout << "旋转矩阵T = " << std::endl << T << std::endl;
Vector2d m = this->pointByMatrix();
m = T * m; //貌似只能T * m
this->setByMatrix(m);
}
/**
* 缩放(比例变换), zx分别表示x轴缩放比例,zy表示y轴缩放比例
*/
void Point::zoom(double zx, double zy) {
Matrix2d T;
T << zx, 0, 0, zy;
Vector2d result = T * this->pointByMatrix();
this->setByMatrix(result);
}
Vector2d Point::pointByMatrix() {
Vector2d a(this->x, this->y);
return a;
}
void Point::setByMatrix(Vector2d m) {
this->x = m(0);
this->y = m(1);
}
定义了一个简单的Shape图形类。
shape.h
#include "Point.h"
class Shape {
private:
const char *shapeName;
int pointNum;
Point *pointArr;
public:
Shape(Point *arr, int num, const char *name);
void printShape();
void rotateShape(double angle);
void moveShape(double x, double y);
void zoomShape(double zx, double zy);
~Shape();
};
shape.cpp
#include "Shape.h"
Shape::Shape(Point *arr, int num, const char *name) {
this->pointArr = arr;
this->pointNum = num;
this->shapeName = name;
}
void Shape::moveShape(double x, double y) {
printf("你输入的指令为 move, 移动距离: (%lf, %lf)\n", x, y);
for (int i = 0; i < this->pointNum; ++i) {
this->pointArr[i].move(x, y);
}
}
void Shape::rotateShape(double angle) {
printf("你输入的指令为 rotate, 旋转角度: %lf\n", angle);
for (int i = 0; i < this->pointNum; ++i) {
this->pointArr[i].rotate(angle);
}
}
void Shape::zoomShape(double zx, double zy) {
printf("你输入的指令为 zoom, 缩放比例为: (%lf, %lf)\n", zx, zy);
for (int i = 0; i < this->pointNum; ++i) {
this->pointArr[i].zoom(zx, zy);
}
}
void Shape::printShape() {
printf("图形%s的%d个点坐标分别为", this->shapeName, this->pointNum);
for (int i = 0; i < this->pointNum - 1; ++i) {
this->pointArr[i].print();
printf(",");
}
this->pointArr[(this->pointNum - 1)].print();
printf("\n");
}
Shape::~Shape() {
printf("调用Helper析构函数\n");
}
运行结果为:

源码提交Github
Eigen库实现简单的旋转、平移操作的更多相关文章
- Eigen库矩阵运算使用方法
Eigen库矩阵运算使用方法 Eigen这个类库,存的东西好多的,来看一下主要的几个头文件吧: ——Core 有关矩阵和数组的类,有基本的线性代数(包含 三角形 和 自伴乘积 相关),还有相应对数组的 ...
- 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍
GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...
- d3.js封装文本实现自动换行和旋转平移等功能
我们下面话不多说,本文主要介绍的是利用D3.js封装文本实现自动换行功能的步骤,下面来一起看看吧. 一.引用 multext.js 文件 multext.js function appendMulti ...
- C++标准库<string>简单总结
C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...
- Linq to SQL 简单的增删改操作
Linq to SQL 简单的增删改操作. 新建数据库表tbGuestBook.结构如下: 新建web项目,完成相应的dbml文件.留言页面布局如下 <body> <form id= ...
- NDK 开发实例二(添加 Eigen库)
上一篇,我已经阐述了如何创建一个简单的NDK实例: NDK 开发实例一(Android.mk环境配置下) 在上一篇的基础上,我们来添加Eigen库,然后做一个简单实例. Eigen是一个高层次的C + ...
- C++标准库vector类型的使用和操作总结
vector是一种类型对象的集合,它是一种顺序容器,容器中的所有对象必须都是同一种类型.想了解顺序容器的更多内容:C++顺序容器知识总结.vector的对象是可以动态生长的,这说明它在初始化时可以不用 ...
- app 下载更新 file-downloader 文件下载库的简单介绍和使用
app 下载更新 file-downloader 文件下载库的简单介绍和使用 今天介绍一个下载库:file-downloader 文件下载库 说明: * 本文内容来自原 file-downloader ...
- Eigen库和STL容器冲突问题
博客参考:https://blog.csdn.net/huajun998/article/details/54311561 在程序中想使用类似于如下的容器 std::vector<Eigne:: ...
随机推荐
- Android事件
1.Java package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; impo ...
- JQuery 的几个有用的技巧
JQuery代码 /* 新窗口打开链接:JQuery filter attr * 禁止鼠标弹出右键菜单:DOM contextmenu * 回到页面顶端:DOM scrollTo * 动态更换Css样 ...
- 第一章 Collections 类、泛型类和Timing类概述
摘抄<数据结构与算法(C#语言描述)> 删除很多废话 1.1群集(collection)的定义 群集是一种结构化的数据类型.存储数据,并且提供数据的添.删.改操作,以及对群集不同属性值的设 ...
- 第一章:1-06、 试将TCP/IP和OSI的体系结构进行比较。讨论其异同之处?
<计算机网络>谢希仁著第四版课后习题答案答:(1)OSI和TCP/IP的相同点是二者均采用层次结构,而且都是按功能分层.(2)OSI和TCP/IP的不同点:①OSI分七层,自下而上分为物理 ...
- c# 实现串口编程-操作LED屏幕
串口编程主要用到SerialPort这个类,主要实现对串口发送字节数组然后点阵屏显示相关信息,其实这个功能很简单下面给大家把整体思路用流程图展现如下:. 其实整体思路就如流程图.下面是整个流程图的一个 ...
- spark性能调优:资源优化
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- php不使用插件导出excel
php不使用插件导出excel的简单方法,首先获取需要导出的数据的数组,数组的格式在下面. 之后就是定义文件名称和需要导出的excel的样式,最后就是循环数组,输出数据了 代码: $filename= ...
- .NET软件汉化小实例
Author:KillerLegend Date:2014.6.18 From:http://www.cnblogs.com/killerlegend/p/3795577.html 好的,今天我们来汉 ...
- PHP-You don’t have permissions to access xxx on this server!
问题如下图: 如果你是想要查看目录下的每一个文件,那么你需要修改一下httpd-conf配置文件,也就是apache的配置文件,以phpStudy2013为例,如下图打开: 然后找到如下部分,添加 ...
- PHP错误处理
错误的分类: 1.语法错误 2.运行时错误 3.逻辑错误 调试方法:1.注释法 2.输出法 error_reporting(E_ALL & ~E_NOTICE & ~E_WAR ...