case 1: du.MoveTo(p_point1); du.LineTo(p_point2); pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2); /*m_ptrArray.Add(pGraph);*/ break; case 2: du.Rectangle(CRect(p_point1, p_point2)); pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2); /*m_ptrArray.Add(pGraph);*/ break; case 3: du.Ellipse(CRect(p_point1, p_point2)); pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2); /*m_ptrArray.Add(pGraph);*/ break; case 4: int midX = (p_point1.x + p_point2.x) / 2; int midY = (p_point1.y + p_point2.y) / 2; CPoint ptTop(midX, p_point1.y); CPoint ptRight(p_point2.x, midY); CPoint ptBottom(midX, p_point2.y); CPoint ptLeft(p_point1.x, midY); // 绘制菱形 CPoint pts[] = { ptTop, ptRight, ptBottom, ptLeft }; du.Polygon(pts, 4); pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2); /*m_ptrArray.Add(pGraph);*/ break; case 5:能否延续写出第五个事件,要求通过两点实现画出一个圆
时间: 2024-03-18 16:43:18 浏览: 92
当pchoice为5时,可以通过以下代码实现画出一个圆:
```
case 5:
int radius = sqrt(pow((p_point2.x - p_point1.x), 2) + pow((p_point2.y - p_point1.y), 2)); // 计算圆的半径
du.Ellipse(CRect(p_point1.x - radius, p_point1.y - radius, p_point1.x + radius, p_point1.y + radius)); // 绘制圆形
pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2);
/*m_ptrArray.Add(pGraph);*/
break;
```
在这段代码中,我们使用两点的坐标计算出圆的半径,然后通过CRect类构造一个矩形,矩形的左上角坐标为(p_point1.x - radius, p_point1.y - radius),右下角坐标为(p_point1.x + radius, p_point1.y + radius),这样就可以绘制出一个圆形。最后,我们创建一个P_Picture对象,并将其添加到m_ptrArray数组中(如果需要的话)。
相关问题
case 5: //圆 radius = abs(p_point1.x - p_point2.x) / 2; centerX = min(p_point1.x, p_point2.x) + radius; centerY = min(p_point1.y, p_point2.y) + radius; du.Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius); pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2); m_ptrArray.Add(pGraph);你能否修改case5之后的代码来实现一种同样能绘画出圆的方法
当然可以,以下是一个实现绘画圆的新代码:
```cpp
case 5:
// 圆
radius = abs(p_point1.x - p_point2.x) / 2;
centerX = min(p_point1.x, p_point2.x) + radius;
centerY = min(p_point1.y, p_point2.y) + radius;
// 绘制圆的边框和内部
CBrush brush(p_color);
CBrush *oldBrush = du.SelectObject(&brush);
CPen pen(PS_SOLID, 1, p_color);
CPen *oldPen = du.SelectObject(&pen);
du.Ellipse(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
du.SelectObject(oldPen);
du.SelectObject(oldBrush);
pGraph = new P_Picture(pchoice, p_color, p_brush, p_point1, p_point2);
m_ptrArray.Add(pGraph);
break;
```
在这个新代码中,我们首先创建了一个与画笔颜色相同的画笔 pen 和一个与画刷颜色相同的画刷 brush,并将它们分别选入画板中。然后,我们使用 du.Ellipse() 函数绘制圆的边框和内部。最后,我们将画刷和画笔恢复回去,否则可能会对后续的绘画产生影响。
阅读全文