栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

已知三点算同距的点

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

已知三点算同距的点

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

#include "qcustomchart.h"
#include 


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)
{
    
    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()<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;idrawLine(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;idrawLine(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();
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号