//2018-09-08-fourmi

/*************************include head files************************************************/
#include<iostream>
#include<math.h>
#include<vector>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<fstream>
/*******************************************************************************************/ /**********************Define variables*****************************************************/
#define pi 3.1415926
#define POINT pair<double,double>
#define VECT vector<vector<double> >
const int MAX_ITER=100000;
const double eps=0.0000001;
/*******************************************************************************************/ using namespace std; /*****************************Vector_Transformation****************************************/
POINT Vector_Transformation(POINT &pt,double num1,double num2,double num3,double num4)
{
POINT result;
result.first=pt.first*num1+pt.second*num3;
result.second=pt.first*num2+pt.second*num4; return result; }
/*******************************************************************************************/ /***************************FUNCTIONS_ABOUT_SVD*********************************************/
double get_norm(double *x, int n){
double r=0; for(int i=0;i<n;i++)
r+=x[i]*x[i];
return sqrt(r);
} double normalize(double *x, int n){
double r=get_norm(x,n); if(r<eps)
return 0;
for(int i=0;i<n;i++)
x[i]/=r;
return r;
} inline double product(double*a, double *b,int n){
double r=0; for(int i=0;i<n;i++)
r+=a[i]*b[i];
return r;
} void orth(double *a, double *b, int n){//|a|=1
double r=product(a,b,n); for(int i=0;i<n;i++)
b[i]-=r*a[i]; } bool svd(VECT A, int K, VECT &U, vector<double> &S, VECT &V){
int M=A.size();
int N=A[0].size();
double *left_vector=new double[M];
double *next_left_vector=new double[M];
double *right_vector=new double[N];
double *next_right_vector=new double[N];
double diff=1;
double r=-1;
int col=0; U.clear();
V.clear();
S.clear();
S.resize(K,0);
U.resize(K);
for(int i=0;i<K;i++)
U[i].resize(M,0);
V.resize(K);
for(int i=0;i<K;i++)
V[i].resize(N,0); for(int col=0;col<K;col++){ while(1){
for(int i=0;i<M;i++)
left_vector[i]= (float)rand() / RAND_MAX;
if(normalize(left_vector, M)>eps)
break;
} for(int iter=0;diff>=eps && iter<MAX_ITER;iter++){
memset(next_left_vector,0,sizeof(double)*M);
memset(next_right_vector,0,sizeof(double)*N);
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_right_vector[j]+=left_vector[i]*A[i][j]; r=normalize(next_right_vector,N);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&V[i][0],next_right_vector,N);
normalize(next_right_vector,N); for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_left_vector[i]+=next_right_vector[j]*A[i][j];
r=normalize(next_left_vector,M);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&U[i][0],next_left_vector,M);
normalize(next_left_vector,M);
diff=0; for(int i=0;i<M;i++){
double d=next_left_vector[i]-left_vector[i];
diff+=d*d;
} memcpy(left_vector,next_left_vector,sizeof(double)*M);
memcpy(right_vector,next_right_vector,sizeof(double)*N);
} if(r>=eps){
S[col]=r;
memcpy((char *)&U[col][0],left_vector,sizeof(double)*M);
memcpy((char *)&V[col][0],right_vector,sizeof(double)*N);
}else{
cout<<r<<endl;
break;
}
} delete [] next_left_vector;
delete [] next_right_vector;
delete [] left_vector;
delete [] right_vector; return true;
}
/*******************************************************************************************/ /**********************GET_THE_BIGGEST_SINGULAR_VALUE***************************************/
vector<double> GET_THE_BIGGEST_SINGULAR_VALUE(POINT vec,int m,int n,int k)
{
//分解一个1*2的矩阵A,求其前1个奇异值和奇异向量 VECT A;
A.resize(m); for(int i=0;i<m;i++)
{
A[i].resize(n);
}
A[0][0]=vec.first;
A[0][1]=vec.second; VECT U;
vector<double> S;
VECT V;
svd(A,k,U,S,V);
return S;
}
/*******************************************************************************************/ /************************CAL_THIRD_AND_FORTH_POINTS*****************************************/
POINT * cal_reset_TWO_points(POINT &pt1,POINT &pt2)
{
static POINT arr[2];
POINT vecFromSecToFirst,vec,pt3,pt4;
int x1,y1,x2,y2,exemplarStart,exemplarEnd;
double distance,angle,s,sideLength;
double slantAngleInRadians = (double(90)/180)*pi;
double LongSideMin=47.9042;
double verticalPSSideLength=195;
double parallelPSSideLength=83;
int matrix_rows=1;
int matrix_cols=2;
int top_k_max=1; x1=pt1.first;
y1=pt1.second;
x2=pt2.first;
y2=pt2.second; distance = sqrt(pow((x1-x2),2)+pow((y1-y2),2)); if (distance < LongSideMin) //说明是短库位
sideLength = verticalPSSideLength;
else //说明是平行长库位
sideLength = parallelPSSideLength; vecFromSecToFirst.first=pt1.first-pt2.first;
vecFromSecToFirst.second=pt1.second-pt2.second;
vec=Vector_Transformation(vecFromSecToFirst,cos(slantAngleInRadians),-sin(slantAngleInRadians), sin(slantAngleInRadians), cos(slantAngleInRadians)); if ((pt2.first-pt1.first)==0)
{
angle=pi/2;
}
else
{
angle=abs(atan2(vecFromSecToFirst.second,vecFromSecToFirst.first));
} s=GET_THE_BIGGEST_SINGULAR_VALUE(vec,matrix_rows,matrix_cols,top_k_max)[0];
vec.first=vec.first/s;
vec.second=vec.second/s; pt3.first=pt2.first+vec.first*sideLength;
pt3.second=pt2.second+vec.second*sideLength;
pt4.first=pt1.first+vec.first*sideLength;
pt4.second=pt1.second+vec.second*sideLength;
arr[0]=pt3;
arr[1]=pt4; return arr;
}
/*******************************************************************************************/ /*****************************************MAIN()********************************************/
int main()
{ POINT * arr;
POINT pt1(171,145);
POINT pt2(171,213);
arr=cal_reset_TWO_points(pt1,pt2);
std::cout<<arr[0].first<<" "<<arr[0].second<<std::endl;
std::cout<<arr[1].first<<" "<<arr[1].second<<std::endl; return 0; }
/*******************************************************************************************/

  

c++实现 给定直角停车位两个点,求取剩余两点坐标。的更多相关文章

  1. 对于给定的整数集合S,求出最大的d,使得a+b+c=d。

    对于给定的整数集合S,求出最大的d,使得a+b+c=d.a,b,c,d互不相同,且都属于S.集合的元素个数小于等于2000个,元素的取值范围在[-2^28,2^28 - 1],假定可用内存空间为100 ...

  2. 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离。显示为公里、米

    /** * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离 * @param array $point_1 第1个点的x,y坐标 array( ...

  3. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

  4. C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)

    获取随机数 举例:0-9 Random random = new Random(); int j = random.Next(0, 9); 0.1两个值被取值的概率相等 int a = Math.Ab ...

  5. SELECT INTO和INSERT INTO SELECT的区别 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对 C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等) C# MD5 加密,解密 C#中DataTable删除多条数据

    SELECT INTO和INSERT INTO SELECT的区别   数据库中的数据复制备份 SELECT INTO: 形式: SELECT value1,value2,value3 INTO Ta ...

  6. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...

  7. python中对两个 list 求交集,并集和差集

    python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3 ...

  8. R语言两种方式求指定日期所在月的天数

                 R语言两种方式求指定日期所在月的天数 days_monthday<-function(date){ m<-format(date,format="%m& ...

  9. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能

    权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...

随机推荐

  1. linux 下为qtcreator 添加排版工具

    1. 下载astyle.   http://sourceforge.net/projects/astyle 这里可以下载最新版本, 目前是3.1  下载文件astyle_3.1_linux.tar.g ...

  2. 2018 “百度之星”程序设计大赛 - 初赛(A)

    第二题还算手稳+手快?最后勉强挤进前五百(期间看着自己从两百多掉到494名) 1001  度度熊拼三角    (hdoj 6374) 链接:http://acm.hdu.edu.cn/showprob ...

  3. python使用PDB进行调试

    参考链接:https://www.cnblogs.com/xiaohai2003ly/p/8529472.html 调入包:import pdb 调试时的一些命令: (1)p  变量名:查看变量:(p ...

  4. ARM的Jazelle技术【转】

    转自:https://blog.csdn.net/ken_yjj/article/details/6797290 Come From: http://www.arm.com/zh/products/p ...

  5. CGI,FastCGI,PHP-CGI与PHP-FPM区别详解【转】

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  6. Keepalived详解(二):Keepalived安装与配置【转】

    一.Keepalived安装与配置: 1.Keepalived的安装过程: Keepalived的安装非常简单,本实例以源码安装讲解: Keepalived的官方网址:http://www.keepa ...

  7. VC常用小知识

    (1) 如何通过代码获得应用程序主窗口的 指针?主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现.AfxGetMainWnd() ->Sho ...

  8. 题解-POI2009 WSP-Island

    Problem bzoj1137 题意概要:给定一个凸多边形坐标.点按顺时针编号 \(1\) 到 \(n\).任意两点之间都有一条长度为欧氏距离的边相连.边相交处可以自由穿行.有 \(m\) 条边不能 ...

  9. Ubuntu14下nginx服务器链接PHP

    报错:nginx下无法打开php,报错[error] 5040#0: *1 connect() failed (111: Connection ref ... server { listen ; #l ...

  10. 安装python2、python3

    先安装python2: python安装 D:\Python27 目录下的 "python.exe" 重命名为 "python2.exe",则在cmd中输入 p ...