QPolygon/QPolygonF方法功能(QT5.12)


//for starf study

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QDebug>
#include<QPainter>
#include<QPolygon>
#include<QPolygonF>
#include<QRect>
#include<QPoint>


//QPolygon类使用整数精度提供点向量。(多边形类)
//QPolygon对象的数据结构是QVector<QPoint>
//所以向QPolygon添加点的最简单方法是使用QVector的流式操作符,如:
/*
         QPolygon polygon;
         polygon << QPoint(10, 20) << QPoint(20, 30);

*/
//QPolygonF 的构成参数的点或者矩形等,可以使用小数
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{

    QPolygon polygon;
    polygon << QPoint(10, 20) << QPoint(20, 30);
    QPolygon poly2 = QPolygon(QRect(20,20,50,60), false);
        //从给定的矩形构造多边形。
        //参数2为false,则多边形只包含按顺时针顺序排列的矩形的四个点,没有封闭
        //参数2为true,则多边形包含5个点,第5个点是左上角的点,完成闭环
    QRect bound = poly2.boundingRect();//返回多边形的边界矩形,如果多边形为空,则返回QRect(0,0,0,0)。
    qDebug()<<poly2.containsPoint(QPoint(25,25),Qt::WindingFill);//根据指定的填充规则,如果给定点位于多边形内部,则返回true;
    qDebug()<<poly2.intersects(polygon); //如果当前多边形和给定多边形(实参)有任何点相交,则返回true。
    int x,  y;
    poly2.point(3,&x,&y);//将给定索引处的点坐标提取到*x和*y(如果它们是有效指针)。
    //QPoint QPolygon::point(int index) const
    polygon.putPoints(15,15,25,25,35,35,45,45);//将Points点从变量参数列表复制到给定索引中的多边形中。
    //void QPolygon::putPoints(int index, int nPoints, const QPolygon &fromPolygon, int fromIndex = 0)
    poly2.setPoint(3, 77,77); //将第三个点设置为77,77
    //void QPolygon::setPoint(int index, const QPoint &point)
    static const int points[] = { 10, 20, 30, 40 };
    polygon.setPoints(2, points); //将多边形的大小调整为nPoints,并用给定的点填充它。
    //void QPolygon::setPoints(int nPoints, int firstx, int firsty, ...)
    QPolygon po = poly2.subtracted(polygon);//返回从该多边形中减去r的多边形。
    poly2.swap(polygon);//对象交换
    poly2.translate(10,10);//平移
    //void QPolygon::translate(const QPoint &offset)
    QPolygon Tr = poly2.translated(19,19);
        //平移,然后生成新对象并且返回
    //QPolygon QPolygon::translated(const QPoint &offset) const
    QPolygon uni = poly2.united(polygon); //返回一个多边形,该多边形是该多边形和r的并集。
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值