
把画家(坐标原点) 移动 到如图
然后画家旋转180度
最重要的是切点坐标
根据圆心坐标,原点坐标,计算切点坐标
1.用QPainterPath绘制出一个圆
2.用它减去三角形路径
3.用画刷刷出上半部分,下半部分

代码太乱,没啥参考性,请自行绘制
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void paintEvent ( QPaintEvent * event );
QPointF calculateTangentCoordinates(QPointF pointf,double R);
void dreaScanPicture(QPainter &p,int length,int r,QColor color);
void setPictureSize(int length,int r,QColor color);
private:
Ui::MainWindow *ui;
int length;
int radius;
double calculate_angle_arc;
QColor color;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QColor color;
color.setRgb(255,245,0,255);
setPictureSize(200,100,color);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setPictureSize(int length, int r, QColor color)
{
this-> length=length;
this-> radius=r;
this-> color=color;
update();
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter p(this);
dreaScanPicture(p,length, radius,color);
}
//计算坐标原点到 圆的切线坐标
QPointF MainWindow::calculateTangentCoordinates(QPointF pointf,double R)
{
double y1=pointf.y();
double r=R;
double angle;
angle=qAsin(r/y1);
double tangentCoordinates_y=qSqrt(y1*y1-r*r)*qCos(angle);
double tangentCoordinates_x=qSqrt(y1*y1-r*r)*qSin(angle);
this->calculate_angle_arc=angle*180/3.14;
return QPointF(tangentCoordinates_x,tangentCoordinates_y);
}
void MainWindow::dreaScanPicture(QPainter &p,int length,int r,QColor color)
{
p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
p.translate(this->width()/2,this->height());
p.rotate(180);
int R=r;
int diameter=2*R;
int height=length;
QPainterPath circle;
circle.addEllipse(QPointF(0,height+R),R,R);
QPointF tangent_coordinates= calculateTangentCoordinates(QPointF(0,height+R),R);
QPainterPath rect;
rect.addRect(R,tangent_coordinates.y(),-diameter,-diameter);
QPainterPath half=circle-rect;
p.fillPath(half,QBrush(color));
// 设置画刷的透明度
QBrush brush;
// brush.setColor(QColor) // 设置透明度为128(范围0-255,0为完全透明,255为完全不透明)
p.setBrush(color);
p.setPen(Qt::NoPen);
QPolygon triangle;
// 计算 过程中
// 由于画笔宽度 误差,角度的计算精度误差,按照 经验 补偿 为5
triangle << QPoint(0,0) <