目錄
問題描述:
問題描述:矩形的一個頂點在原點,已知矩形的寬度W以及高度H,以及矩形和X軸方向的夾角角度angle,求離原點較遠的兩條邊的中心座標點。P(x,y)以及Q(x,y),如下圖所示。
解決方案:
求矩形邊的中心座標點,最簡單的方法就是將其餘三個頂點的座標全部求出來,只要將兩個頂點的座標點相加除2就是中心點的座標。Point1和Point3直接利用三角函數求即可。Point2作一條輔助線,找到和point1的關係即可。
x2 + sin(弧度)*h = x1
y1 + cos(弧度)*h = y2
就能得出如下關係
P.x_ = (2 * cos(angle) * width - sin(angle) * hight) / 2;
P.y_ = (2 * sin(angle) * width + cos(angle) * hight) / 2;
Q.x_ = (cos(angle) * width - 2 * sin(angle) * hight) / 2;
Q.y_ = (sin(angle) * width + 2 * cos(angle) * hight) / 2;
編程測試
設長度和高度都是根號2,角度爲45度,將程序計算結果和實際結果對比一下即可。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592
typedef struct {
float x_;
float y_;
}PointSturuct;
int main() {
float width = sqrt(2);
float hight = sqrt(2);
float angle = 45 * (PI/180); //角度換成弧度制 三角函數入參是弧度值
PointSturuct P, Q;
P.x_ = (2 * cos(angle) * width - sin(angle) * hight) / 2;
P.y_ = (2 * sin(angle) * width + cos(angle) * hight) / 2;
Q.x_ = (cos(angle) * width - 2 * sin(angle) * hight) / 2;
Q.y_ = (sin(angle) * width + 2 * cos(angle) * hight) / 2;
printf("P(x,y) = (%f,%f)\r\n",P.x_,P.y_);
printf("Q(x,y) = (%f,%f)\r\n",Q.x_,Q.y_);
//float x = sin(angle);
//float y = cos(angle);
//printf("sin(angle) = %f\r\ncos(angle) = %f\r\n", x,y);
system("pause");
}