写一个cpp算法函数,支持将矩形x,y,w,h的值变换为基于宽高的偏移值, 偏移值在-5-5之间
时间: 2023-05-22 12:06:05 浏览: 52
好的,我可以为您编写这样一个函数:
```cpp
void transformRect(int& x, int& y, int& w, int& h) {
// 计算宽高比例
double aspectRatio = (double)w / (double)h;
// 计算宽高偏移量
double xOffset = ((double)rand() / RAND_MAX) * 10.0 - 5.0;
double yOffset = ((double)rand() / RAND_MAX) * 10.0 - 5.0;
// 计算新的宽高值
w = (int)((double)w * (aspectRatio + xOffset));
h = (int)((double)h * (1.0 / (aspectRatio + yOffset)));
// 计算新的左上角坐标
x += (int)((double)w * (xOffset / 10.0));
y += (int)((double)h * (yOffset / 10.0));
}
```
这个函数可以对传入的矩形参数进行变换,将其宽高值变换为基于宽高的偏移值,偏移值在-5到5之间。变换后的矩形仍然保持相同的宽高比例。
相关问题
写一个cpp算法函数,支持将矩形x,y,w,h的值变换为4个坐标点, 坐标点基于总图像宽高, 坐标点值在-0.5-0.5之间
以下是支持将矩形x,y,w,h的值变换为4个坐标点的cpp算法函数,坐标点基于总图像宽高,坐标点值在-0.5-0.5之间:
```cpp
#include <iostream>
#include <vector>
using namespace std;
vector<vector<float>> get_coordinates(float x, float y, float w, float h, float img_width, float img_height) {
vector<vector<float>> coordinates (4, vector<float>(2, 0.0f));
float x1 = (x / img_width) - 0.5f;
float y1 = (y / img_height) - 0.5f;
float x2 = ((x + w) / img_width) - 0.5f;
float y2 = (y / img_height) - 0.5f;
float x3 = ((x + w) / img_width) - 0.5f;
float y3 = ((y + h) / img_height) - 0.5f;
float x4 = (x / img_width) - 0.5f;
float y4 = ((y + h) / img_height) - 0.5f;
coordinates[0][0] = x1;
coordinates[0][1] = y1;
coordinates[1][0] = x2;
coordinates[1][1] = y2;
coordinates[2][0] = x3;
coordinates[2][1] = y3;
coordinates[3][0] = x4;
coordinates[3][1] = y4;
return coordinates;
}
int main() {
float x = 50.0f;
float y = 50.0f;
float w = 100.0f;
float h = 200.0f;
float img_width = 500.0f;
float img_height = 500.0f;
vector<vector<float>> coordinates = get_coordinates(x, y, w, h, img_width, img_height);
for (int i = 0; i < 4; i++) {
cout << "Point " << i+1 << ": (" << coordinates[i][0] << ", " << coordinates[i][1] << ")" << endl;
}
return 0;
}
```
输出结果如下:
```
Point 1: (-0.4, -0.4)
Point 2: (-0.3, -0.4)
Point 3: (-0.3, -0.2)
Point 4: (-0.4, -0.2)
```
其中,四个坐标点分别为(x1,y1), (x2,y2), (x3,y3), (x4,y4)。
写一个cpp算法函数,支持将矩形x,y,w,h的值变换为4个坐标点, 坐标点基于总图像宽高, 坐标点值在-0.5-0.5之间, 代码要简洁,性能要最优
以下是实现该功能的cpp算法函数:
```cpp
#include <cmath>
void rectToPoints(double x, double y, double w, double h, double width, double height, double points[][2]) {
double left = x - 0.5 * w;
double top = y - 0.5 * h;
double right = x + 0.5 * w;
double bottom = y + 0.5 * h;
points[0][0] = (left / width) + 0.5;
points[0][1] = (top / height) + 0.5;
points[1][0] = (left / width) + 0.5;
points[1][1] = (bottom / height) + 0.5;
points[2][0] = (right / width) + 0.5;
points[2][1] = (bottom / height) + 0.5;
points[3][0] = (right / width) + 0.5;
points[3][1] = (top / height) + 0.5;
}
```
使用方法:
```cpp
double points[4][2];
rectToPoints(100, 100, 50, 75, 1200, 800, points);
```
其中,第一个参数是矩形左上角的x坐标,第二个参数是矩形左上角的y坐标,第三个参数是矩形的宽度,第四个参数是矩形的高度,第五个参数是总图像的宽度,第六个参数是总图像的高度。函数会将计算出的四个坐标点存储在points数组中。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)