POINT points[] = { {diamond_1_point.x,diamond_1_point.y}, {diamond_2_point.x,diamond_2_point.y}, {diamond_3_point.x,diamond_3_point.y}, {diamond_4_point.x,diamond_4_point.y}, };修正这段代码
时间: 2024-03-14 08:42:56 浏览: 16
在这段代码中,假设 `diamond_1_point`、`diamond_2_point`、`diamond_3_point` 和 `diamond_4_point` 都是 `CPoint` 类型的变量,表示钻石形状的四个顶点。如果要将这四个顶点存储到 `POINT` 类型的数组中,代码应该如下所示:
```C++
POINT points[] = {
{ diamond_1_point.x, diamond_1_point.y },
{ diamond_2_point.x, diamond_2_point.y },
{ diamond_3_point.x, diamond_3_point.y },
{ diamond_4_point.x, diamond_4_point.y }
};
```
这里使用了 C++ 的列表初始化语法,将四个 `CPoint` 类型的变量转换成 `POINT` 类型的数组,并初始化了数组中的每个元素。注意,这里 `POINT` 类型和 `CPoint` 类型是等价的,因为它们都包含了 `x` 和 `y` 两个成员变量。
相关问题
CDiamond::CDiamond() { } void CDiamond::Set_first_point(CPoint p) { diamond_first_point = p; } void CDiamond::Set_end_point(CPoint p) { diamond_end_point = p; } void CDiamond::Get_other_point(CPoint p) { diamond_1_point.x = diamond_first_point.x; diamond_1_point.y = (diamond_first_point.y + diamond_end_point.y) / 2; diamond_3_point.x = diamond_end_point.x; diamond_3_point.y = diamond_1_point.y; diamond_2_point.y = diamond_first_point.y; diamond_2_point.x = (diamond_first_point.x + diamond_end_point.x) / 2; diamond_4_point.y = diamond_end_point.y; diamond_3_point.x = diamond_2_point.x; } POINT points[] = { { diamond_1_point.x,diamond_1_point.y}, {diamond_2_point.x,diamond_2_point.y}, {diamond_3_point.x,diamond_3_point.y}, {diamond_4_point.x,diamond_4_point.y} };修正这段代码
在这段代码中,`diamond_1_point`、`diamond_2_point`、`diamond_3_point` 和 `diamond_4_point` 都是 `CPoint` 类型的变量,表示钻石形状的四个顶点,且 `diamond_first_point` 和 `diamond_end_point` 也是 `CPoint` 类型的变量,表示图形的起点和终点。
如果要将这四个顶点存储到 `POINT` 类型的数组中,需要在计算出 `diamond_1_point`、`diamond_2_point`、`diamond_3_point` 和 `diamond_4_point` 的值之后,再将其存储到数组中。因此,你需要将 `POINT points[]` 的定义放到 `Get_other_point` 函数中,代码应该如下所示:
```C++
void CDiamond::Get_other_point(CPoint p)
{
diamond_1_point.x = diamond_first_point.x;
diamond_1_point.y = (diamond_first_point.y + diamond_end_point.y) / 2;
diamond_3_point.x = diamond_end_point.x;
diamond_3_point.y = diamond_1_point.y;
diamond_2_point.y = diamond_first_point.y;
diamond_2_point.x = (diamond_first_point.x + diamond_end_point.x) / 2;
diamond_4_point.y = diamond_end_point.y;
diamond_4_point.x = diamond_2_point.x;
POINT points[] = {
{ diamond_1_point.x, diamond_1_point.y },
{ diamond_2_point.x, diamond_2_point.y },
{ diamond_3_point.x, diamond_3_point.y },
{ diamond_4_point.x, diamond_4_point.y }
};
}
```
这里将 `POINT points[]` 的定义移到了 `Get_other_point` 函数中,并且在计算 `diamond_1_point`、`diamond_2_point`、`diamond_3_point` 和 `diamond_4_point` 的值之后,直接将它们存储到了数组中。
class Point: def __init__(self, x, y): self.__x = x self.__y = y def __add__(self, other): x = self.__x + other.__x y = self.__y + other.__y return Point(x, y) def __sub__(self, other): x = self.__x - other.__x y = self.__y - other.__y return Point(x, y) def __mul__(self, a): x = self.__x * a y = self.__y * a return Point(x, y) def __truediv__(self, a): x = self.__x / a y = self.__y / a return Point(x, y) r1 = Point(10, 20) r2 = Point(5, 5) print(r1+r2) print(r1-r2) print(r1*2) print(r1/2)代码改进,输出向量坐标
为了输出向量坐标,我们可以在 Point 类中添加一个方法来返回坐标的元组,如下所示:
```python
class Point:
def __init__(self, x, y):
self.__x = x
self.__y = y
def __add__(self, other):
x = self.__x + other.__x
y = self.__y + other.__y
return Point(x, y)
def __sub__(self, other):
x = self.__x - other.__x
y = self.__y - other.__y
return Point(x, y)
def __mul__(self, a):
x = self.__x * a
y = self.__y * a
return Point(x, y)
def __truediv__(self, a):
x = self.__x / a
y = self.__y / a
return Point(x, y)
def get_coordinates(self):
return (self.__x, self.__y)
```
然后我们就可以使用这个方法来输出向量坐标了,示例如下:
```python
r1 = Point(10, 20)
r2 = Point(5, 5)
print(r1+r2.get_coordinates()) # 输出 (15, 25)
print(r1-r2.get_coordinates()) # 输出 (5, 15)
print(r1*2).get_coordinates()) # 输出 (20, 40)
print(r1/2).get_coordinates()) # 输出 (5.0, 10.0)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)