亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

QPainter畫一個3D正方體的方法

發布時間:2020-08-21 10:06:35 來源:億速云 閱讀:232 作者:小新 欄目:開發技術

這篇文章主要介紹QPainter畫一個3D正方體的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

QPainter畫一個3D正的具體代碼,供大家參考:

QPainter畫一個3D正方體的方法

QPainter畫一個3D正方體的方法

My3DBox .h

#include <Eigen/Geometry>
using namespace Eigen;

#define SQUARE_LENGTH 200//是一個邊長200的正方體
#define CAMERA_DISTANCE 300//視點距離

class My3DBox : public QWidget
{
  Q_OBJECT
public:
  explicit My3DBox(QWidget *parent = 0);
protected:
  QPoint m_mousePos;
  Vector3d m_vector[8];//正方形8個點坐標

  void mouseMoveEvent(QMouseEvent *);
  void paintEvent(QPaintEvent *);

  void drawPlane(const QPoint *points,QPainter &painter);
  Matrix3d getMartix();
  QPoint ToPoint(Vector3d vector);
  void drawUp(QPoint *points,QPainter &painter);//正方體面的繪制
  void drawDown(QPoint *points,QPainter &painter);
  void drawLeft(QPoint *points,QPainter &painter);
  void drawRight(QPoint *points,QPainter &painter);
};

My3DBox .cpp

#include "widget.h"

#define PI 3.1415926

My3DBox::My3DBox(QWidget *parent) :
  QWidget(parent),m_mousePos(0,0)
{
  setMouseTracking(true);//在任何情況下啟用mouseMoveEvent
  setGeometry(400,200,500,500);

  m_vector[0] << -100,100,100;//設置正方體頂點初始位置
  m_vector[1] << 100,100,100;
  m_vector[2] << -100,-100,100;
  m_vector[3] << 100,-100,100;
  m_vector[4] << -100,100,-100;
  m_vector[5] << 100,100,-100;
  m_vector[6] << -100,-100,-100;
  m_vector[7] << 100,-100,-100;
}

void My3DBox::mouseMoveEvent(QMouseEvent *e)//此處把坐標原點從左上角變換至屏幕中心
{
  if(e->pos().x() < 0)
    m_mousePos.setX(-width()/2);
  else if(e->pos().x() > width())
    m_mousePos.setX(width()/2);
  else
    m_mousePos.setX(e->pos().x() - width()/2);

  if(e->pos().y() < 0)
    m_mousePos.setY(height()/2);
  else if(e->pos().y() > height())
    m_mousePos.setY(-height()/2);
  else
    m_mousePos.setY(-e->pos().y() + height()/2);

  update();
}

void My3DBox::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  painter.setRenderHint(QPainter::SmoothPixmapTransform);
  painter.setPen(Qt::NoPen);

  painter.fillRect(rect(),QColor(3,22,52));

  Vector3d vector[8];
  Matrix3d matrix = getMartix();//獲取變換矩陣

  QPoint points[8];//正面0123 左面4062 右面1537 上面4501 下面 2367(以觀察者的方向為主)
  for(int i = 0;i < 8;++i)//計算變換后坐標
  {
    vector[i] = matrix*m_vector[i];
    points[i] = ToPoint(vector[i]);

    points[i].setX(points[i].x()+width()/2);//將坐標系原點變換至左上角
    points[i].setY(-points[i].y()+height()/2);
  }

  //一共要畫5個面,最后畫正面
  if(qAbs(m_mousePos.x()) > qAbs(m_mousePos.y()))
  {
    if(m_mousePos.x() > 0)
    {
      if(m_mousePos.y() > 0)
        drawDown(points,painter);
      else
        drawUp(points,painter);
      drawLeft(points,painter);
    }
    else
    {
      if(m_mousePos.y() > 0)
        drawDown(points,painter);
      else
        drawUp(points,painter);
      drawRight(points,painter);
    }
  }
  else
  {
    if(m_mousePos.y() > 0)
    {
      if(m_mousePos.x() > 0)
        drawLeft(points,painter);
      else
        drawRight(points,painter);
      drawDown(points,painter);
    }
    else
    {
      if(m_mousePos.x() > 0)
        drawLeft(points,painter);
      else
        drawRight(points,painter);
      drawUp(points,painter);
    }
  }
  //畫正面
  QPoint point[4] =
  {
    points[0],
    points[1],
    points[3],
    points[2]
  };
  drawPlane(point,painter);
}

void My3DBox::drawPlane(const QPoint *points,QPainter &painter)
{
  QLinearGradient linearGradient(points[1],points[3]);//設置漸變色
  linearGradient.setColorAt(0.0,QColor(150,150,250));
  linearGradient.setColorAt(1.0,QColor(170,170,255));
  painter.setBrush(QBrush(linearGradient));
  painter.drawConvexPolygon(points,4);
}

Matrix3d My3DBox::getMartix()
{
  double unit_x = 0;//同方向單位向量的x和y
  double unit_y = 0;
  double z = 0;

  if(m_mousePos.x() != 0 || m_mousePos.y() != 0)
  {
    unit_x = m_mousePos.x()/qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y());
    unit_y = m_mousePos.y()/qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y());
    z = qSqrt(m_mousePos.x()*m_mousePos.x() + m_mousePos.y()*m_mousePos.y())/qSqrt(width()*width()/4 + height()*height()/4);
  }

  Matrix3d M_Z;
  if(unit_x == 0 && unit_y == 0)
    M_Z << 1,0,0,
        0,1,0,
        0,0,1;
  else
    M_Z << unit_y,-unit_x,0,
        unit_x,unit_y,0,
        0,0,1;

  Matrix3d M_X;
  if(z == 0)
    M_X << 1,0,0,
        0,1,0,
        0,0,1;
  else
    M_X << 1,0,0,
        0,cos(z*PI/2.6),sin(z*PI/2.6),
        0,-sin(z*PI/2.6),cos(z*PI/2.6);//為什么PI/2大于90度?因為沒畫背面所以必須小于90度(偷懶)

  return M_Z.inverse()*M_X*M_Z;
}

QPoint My3DBox::ToPoint(Vector3d vector)//將3d坐標投影至平面上
{
  QPoint point;

  point.setX(CAMERA_DISTANCE*vector[0]/(CAMERA_DISTANCE-vector[2]));
  point.setY(CAMERA_DISTANCE*vector[1]/(CAMERA_DISTANCE-vector[2]));

  return point;
}

void My3DBox::drawUp(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[4],
    points[5],
    points[1],
    points[0]
  };
  drawPlane(point,painter);
}

void My3DBox::drawDown(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[2],
    points[3],
    points[7],
    points[6]
  };
  drawPlane(point,painter);
}

void My3DBox::drawLeft(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[4],
    points[0],
    points[2],
    points[6]
  };
  drawPlane(point,painter);
}

void My3DBox::drawRight(QPoint *points,QPainter &painter)
{
  QPoint point[4] =
  {
    points[1],
    points[5],
    points[7],
    points[3]
  };
  drawPlane(point,painter);
}

以上是QPainter畫一個3D正方體的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

射阳县| 中山市| 茶陵县| 宁波市| 延川县| 个旧市| 和平县| 霍林郭勒市| 河北区| 茶陵县| 涿鹿县| 什邡市| 石家庄市| 察隅县| 庐江县| 准格尔旗| 雷州市| 灵武市| 额尔古纳市| 夏邑县| 囊谦县| 华亭县| 峨边| 积石山| 玉溪市| 全州县| 城口县| 高州市| 枣阳市| 北票市| 佛教| 清涧县| 蓝山县| 哈尔滨市| 永年县| 平凉市| 福安市| 疏勒县| 镇远县| 报价| 三门县|