@SOAR 2016-10-28 11:51 采纳率: 100%
浏览 1174
已采纳

mfc画图时为什么需要首先计算封闭矩形

vc++ 2013 入门经典画图时,为什么构造函数都要进行封闭图形计算?

其中InflateRect()NormalizeRect();函数什么意思啊,重点是为什么要调用它们?
他们起什么作用,请指点迷津,小弟不胜感激。。
代码示例如下

```CEllipse:: CEllipse (const CPoint& center, const CPoint& end, COLORREF color) : CElement(center, color)
{
// Create the rectangle enclosing the ellipse
int halfWidth{ abs(center.x - end.x) }; // Half the width of the defining rectangle
int halfHeight{ abs(center.y - end.y) }; // Half the height of the defining rectangle

m_StartPoint = m_BottomRight = center;
m_StartPoint.Offset(-halfWidth, -halfHeight); // Top left is offset by minus half height and half width
m_BottomRight.Offset(halfWidth, halfHeight); // Bottom right point you add to the center coordinates

// Points must differ by 2 in x & y for valid rectangle
if (m_StartPoint.x - m_BottomRight.x < 2) m_BottomRight.x += 2;
if (m_StartPoint.y - m_BottomRight.y < 2) m_BottomRight.y += 2;

m_EnclosingRect = CRect{ m_StartPoint, m_BottomRight };
m_EnclosingRect.NormalizeRect();
m_EnclosingRect.InflateRect(m_PenWidth, m_PenWidth);
}

CLine::CLine(const CPoint& start, const CPoint& end, COLORREF color) :
CElement { start, color }, m_EndPoint { end } 
{
  // Define the enclosing rectangle
  m_EnclosingRect = CRect { start, end };
  m_EnclosingRect.NormalizeRect();
  m_EnclosingRect.InflateRect(m_PenWidth, m_PenWidth);
}

```CCircle::CCircle(const CPoint& start, const CPoint& end, COLORREF color) : CElement { start, color }
{
  // Calculate the radius using floating-point values
  // because that is required by sqrt() function (in cmath) 
  long radius { static_cast<long> (sqrt(
    static_cast<double>((end.x - start.x)*(end.x - start.x) + (end.y - start.y)*(end.y - start.y)))) };
  if (radius < 1L) radius = 1L;        // Circle radius must be >= 1

  // Define left-top and right-bottom points of rectangle for MM_TEXT mode
  m_StartPoint = CPoint { start.x - radius, start.y - radius };
  m_BottomRight = CPoint { start.x + radius, start.y + radius };

  // Define the enclosing rectangle
  m_EnclosingRect = CRect {m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y };
  m_EnclosingRect.InflateRect(m_PenWidth, m_PenWidth);
}

  • 写回答

2条回答 默认 最新

  • oyljerry 2016-10-28 11:57
    关注

    因为这些API画图形的时候就是根据这个最外侧矩形来组成的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制