欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

已知三点算同距的点

程序员文章站 2022-04-04 09:03:08
...

GetPiontByThree是求解函数本代码是Qt写的,换成其他方式,自行修改

#include "qcustomchart.h"
#include <QDebug>


QCustomChart::QCustomChart(QWidget *parent) :
    QWidget(parent)
{
    posLineState = false;
    leftTitle = "y";
    topTitle = "pos";
    bottomTitle = "x";
    aixs_X_start = 0;
    aixs_X_stop = 10;
    aixs_Y_start = 0;
    aixs_Y_stop = 10;
    x_scale_num = 10;
    y_scale_num = 10;
    posSelect = 0;

    m_pos = QPoint(0,0);
    setDistanceValue(4,4,4,4);


}
QPoint QCustomChart::GetPiontByThree(QPoint p1, QPoint p2, QPoint p3,double distance1,double distance2,double distance3)
{
    /* Math.Pow(y1-Y)+Math.Pow(X-x1)=Math.Pow(D1)
             * Math.Pow(y2-Y)+Math.Pow(X-x2)=Math.Pow(D2)
             * Math.Pow(y3-Y)+Math.Pow(X-x3)=Math.Pow(D3)
             * 1-3.2-3解得:
             * 2 * (p1.X - p3.X)x + 2 * (p1.Y - p3.Y)y = Math.Pow(p1.X, 2) - Math.Pow(p3.X, 2) + Math.Pow(p1.Y, 2) - Math.Pow(p3.Y, 2) + Math.Pow(p3.Distance, 2) - Math.Pow(p1.Distance, 2);
             * 2 * (p2.X - p3.X)x + 2 * (p2.Y - p3.Y)y = Math.Pow(p2.X, 2) - Math.Pow(p3.X, 2) + Math.Pow(p2.Y, 2) - Math.Pow(p3.Y, 2) + Math.Pow(p3.Distance, 2) - Math.Pow(p2.Distance, 2);
             * 简化:
             * 2Ax+2By=C
             * 2Dx+2Ey=F
             * 简化:
             * x=(BF-EC)/(2BD-2AE)
             * y=(AF-DC)/(2AE-2BD)
             */
    double A = p1.x() - p3.x();
    double B = p1.y() - p3.y();
    double C = pow(p1.x(), 2) - pow(p3.x(), 2) + pow(p1.y(), 2) - pow(p3.y(), 2) + pow(distance3, 2) - pow(distance1, 2);
    double D = p2.x() - p3.x();
    double E = p2.y() - p3.y();
    double F = pow(p2.x(), 2) - pow(p3.x(), 2) + pow(p2.y(), 2) - pow(p3.y(), 2) + pow(distance3, 2) - pow(distance2, 2);

    double x = (B * F - E * C) / (2 * B * D - 2 * A * E);
    double y = (A * F - D * C) / (2 * A * E - 2 * B * D);

    QPoint P(x,y);
    return P;
}

QCustomChart::~QCustomChart()
{

}

void QCustomChart::setPosLineState(bool value)
{
    posLineState = value;
    update();
}

void QCustomChart::setPosSelect(int value)
{
    posSelect = value;
}

QPoint QCustomChart::setDistanceValue(double value1, double value2, double value3, double value4)
{
    posLineState = true;
    QPoint p1(0,4);
    QPoint p2(4,0);
    QPoint p3(4,8);
    QPoint p4(8,4);

    QPoint m_pos1;
    QPoint m_pos2;
    QPoint m_pos3;
    QPoint m_pos4;

    //进行计算
    m_pos1 = GetPiontByThree(p1,p2,p3,value1,value2,value3);
    m_pos2 = GetPiontByThree(p2,p3,p4,value2,value3,value4);
    m_pos3 = GetPiontByThree(p3,p4,p1,value3,value4,value1);
    m_pos4 = GetPiontByThree(p4,p1,p2,value4,value1,value2);

//    if(m_pos1.x()>=0&&m_pos1.y()>=0){
//        m_pos = m_pos1;
//    }else if(m_pos2.x()>=0&&m_pos2.y()>=0){
//        m_pos = m_pos2;
//    }else if(m_pos3.x()>=0&&m_pos3.y()>=0){
//        m_pos = m_pos3;
//    }else if(m_pos4.x()>=0&&m_pos4.y()>=0){
//        m_pos = m_pos4;
//    }
    if(posSelect == 0){
         m_pos = m_pos1;
    }
    if(posSelect == 1){
         m_pos = m_pos2;
    }
    if(posSelect == 2){
         m_pos = m_pos3;
    }
    if(posSelect == 3){
         m_pos = m_pos4;
    }
    update();

    qDebug()<<m_pos;
    return m_pos;
}

void QCustomChart::mousePressEvent(QMouseEvent *event)
{
    double pointx = m_width * 0.1;//绘制起点
    double pointy = m_height * 0.9;//绘制起点
    double paintwidth = m_width*0.8;//绘制宽度
    double paintheight= m_height*0.8;//确定坐标轴宽度跟高度 上文定义画布为600X300,宽高依此而定。
    QPoint t_pos = event->pos();
    double heightProportion = paintheight/maxValue;//高度比
    double t_num = pointy - t_pos.y();
}

void QCustomChart::mouseReleaseEvent(QMouseEvent *event)
{


}

void QCustomChart::mouseMoveEvent(QMouseEvent *event)
{

}

void QCustomChart::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    drawBg(&painter);
    drawAixs(&painter);
}

void QCustomChart::resizeEvent(QResizeEvent *event)
{
    m_width = this->width();
    m_height = this->height();
    double pointx = m_width * 0.1;//起点
    double pointy = m_height * 0.9;//起
}

void QCustomChart::drawBg(QPainter *painter)
{
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("white"));
    painter->drawRect(0,0,m_width,m_height);
}

void QCustomChart::drawAixs(QPainter *painter)
{

    painter->save();
    painter->setPen(QColor(128,128,0));
    double pointx = m_width * 0.1;//起点
    double pointy = m_height * 0.9;//起点
    double paintwidth = m_width*0.8;//绘制宽度
    double paintheight = m_height*0.8;//确定坐标轴宽度跟高度 上文定义画布为600X300,宽高依此而定。

    QPoint t_pos;//坐标点位置,
    //这里要转换一下
    t_pos.setX(pointx + m_pos.x()*paintwidth/10);
    t_pos.setY(pointy - m_pos.y()*paintheight/10);


    //坐标线
    painter->drawLine(pointx,pointy,paintwidth+pointx,pointy);//坐标轴x宽度为paintwidth
    painter->drawLine(pointx,pointy - paintheight,pointx,pointy);//坐标轴y高度为paintheight

    //2条红线
    //y
    painter->drawLine(pointx,pointy-(7+1)*paintheight/y_scale_num,paintwidth+pointx,pointy-(7+1)*paintheight/y_scale_num);//坐标轴x宽度为paintwidth
    //x
    painter->drawLine(pointx+(7+1)*paintwidth/x_scale_num,pointy - paintheight,pointx+(7+1)*paintwidth/x_scale_num,pointy);//坐标轴y高度为paintheight

    //四个角红点
    int x1; int x2; int x3; int x4;
    int y1; int y2; int y3; int y4;
    x1 = pointx;
    y1 = pointy;
    x2 = pointx;
    y2 = pointy - paintheight/10*8;
    x3 = pointx + paintwidth/10*8;
    y3 = pointy - paintheight/10*8;
    x4 = pointx + paintwidth/10*8;
    y4 = pointy;

    painter->setBrush(QColor("red"));
    //画圆
    painter->drawEllipse(x1-5,y1-5,10,10);
    painter->drawEllipse(x2-5,y2-5,10,10);
    painter->drawEllipse(x3-5,y3-5,10,10);
    painter->drawEllipse(x4-5,y4-5,10,10);


    //四个中间红点
    int cx1; int cx2; int cx3; int cx4;
    int cy1; int cy2; int cy3; int cy4;
    cx1 = pointx;
    cy1 = pointy - paintheight/10*4;

    cx2 = pointx + paintwidth/10*4;
    cy2 = pointy - paintheight/10*8;

    cx3 = pointx + paintwidth/10*8;
    cy3 = pointy - paintheight/10*4;

    cx4 = pointx + paintwidth/10*4;
    cy4 = pointy;

    painter->setBrush(QColor("red"));
    //画圆
    painter->drawEllipse(cx1-5,cy1-5,10,10);
    painter->drawEllipse(cx2-5,cy2-5,10,10);
    painter->drawEllipse(cx3-5,cy3-5,10,10);
    painter->drawEllipse(cx4-5,cy4-5,10,10);


    //位置标示
    painter->drawText(cx1-5,cy1-5,"1");
    painter->drawText(cx2-5,cy2-5,"2");
    painter->drawText(cx3-5,cy3-5,"3");
    painter->drawText(cx4-5,cy4-5,"4");

    if(posLineState == true){
        painter->setPen(QColor(0,0,0));
        painter->drawEllipse(t_pos.x()-5,t_pos.y()-5,10,10);
        painter->drawText(t_pos.x(),t_pos.y(),"DS");

        //画位置点与四个中心的连线
        painter->drawLine(cx1,cy1,t_pos.x(),t_pos.y());//坐标轴x宽度为paintwidth
        painter->drawLine(cx2,cy2,t_pos.x(),t_pos.y());//坐标轴x宽度为paintwidth
        painter->drawLine(cx3,cy3,t_pos.x(),t_pos.y());//坐标轴x宽度为paintwidth
        painter->drawLine(cx4,cy4,t_pos.x(),t_pos.y());//坐标轴x宽度为paintwidth
    }


    painter->drawText(pointx-10,pointy+15,"0");

    double tmp_x = abs(aixs_X_stop - aixs_X_start)/x_scale_num;
    double tmp_y = abs(aixs_Y_stop - aixs_Y_start)/x_scale_num;
    //画上x轴刻度线
    for(int i=0;i<x_scale_num;i++)//分成10份
    {
        //选取合适的坐标,绘制一段长度为4的直线,用于表示刻度
        painter->drawLine(pointx+(i+1)*paintwidth/x_scale_num,pointy,pointx+(i+1)*paintwidth/x_scale_num,pointy+4);

        painter->drawText(pointx+(i+0.65)*paintwidth/x_scale_num,
                          pointy+15,QString::number(aixs_X_start + tmp_x*(i+1),'f',0));
    }
    //画上y轴刻度线
    for(int i=0;i<y_scale_num;i++)
    {
        //代码较长,但是掌握基本原理即可。
        //主要就是确定一个位置,然后画一条短短的直线表示刻度。
        painter->drawLine(pointx,pointy-(i+1)*paintheight/y_scale_num,
                          pointx-4,pointy-(i+1)*paintheight/y_scale_num);
        //刻度数
        painter->drawText(pointx-40,pointy-(i+0.9)*paintheight/y_scale_num,
                          QString::number(aixs_Y_start + tmp_y*(i+1),'f',0));
    }


    painter->setPen(QColor(152,99,88));
    painter->drawText(m_width*0.45,m_height*0.05,topTitle);
    painter->drawText(m_width*0.45,m_height * 0.975,bottomTitle);
    int y = ( this->height()/ 2);
    painter->rotate(270);
    painter->drawText(-y-6,m_width*0.02,leftTitle);
    painter->restore();
}