gpt4 book ai didi

c++ - 给Qt中距离最小的点一个标志的语义错误

转载 作者:行者123 更新时间:2023-11-28 06:31:08 30 4
gpt4 key购买 nike

我正在做一个 qt 项目。我正在计算用户点击鼠标输入的每个点与我声明的一些虚拟点之间的距离。该点将采用具有最小距离的虚拟点的标志。我的项目中存在语义错误。我试图追踪代码,但我不明白为什么会这样。问题是我总是得到标志 1。当我输入 2 点时,我得到标志 11。它总是 1。

当我调试 2 点时:

138 ,  134 
distance 1 0
distance 2 7
distance 3 14
distance 4 5
distance 5 8.60233
distance 6 14.8661
flag " 1"
183 , 168
distance 1 56.4004
distance 2 52.4786
distance 3 49.2443
distance 4 52.4976
distance 5 48.2597
distance 6 44.7214
flag " 11"

我觉得问题出在这里:

      if (min_distance== distance_pt_1 ){
char_flag=char_flag.append("1");
}
else if (min_distance== distance_pt_2 ){
char_flag=char_flag.append("2");
}
else if (min_distance== distance_pt_3 ){
char_flag=char_flag.append("3");
}
else if (min_distance== distance_pt_4 ){
char_flag=char_flag.append("4");
}
else if (min_distance== distance_pt_5 ){
char_flag=char_flag.append("5");
}
else if (min_distance== distance_pt_6 ){
char_flag=char_flag.append("6");
}

qDebug()<<"flag " <<char_flag;

开头的代码:

 foreach( QGraphicsItem * item, list )
{
min_distance=scene->width();
distance_pt_1=scene->width();
distance_pt_2=scene->width();
distance_pt_3=scene->width();
distance_pt_4=scene->width();
distance_pt_5=scene->width();
distance_pt_6=scene->width();

int xx=item->x();
int yy=item->y();
qDebug()<<xx<<", "<< yy;

distance_pt_1=qSqrt(qPow(xx- pt_1.x(),2)+qPow(yy- pt_1.y(),2));
distance_pt_2=qSqrt(qPow(xx- pt_2.x(),2)+qPow(yy- pt_2.y(),2));
distance_pt_3=qSqrt(qPow(xx- pt_3.x(),2)+qPow(yy- pt_3.y(),2));
distance_pt_4=qSqrt(qPow(xx- pt_4.x(),2)+qPow(yy- pt_4.y(),2));
distance_pt_5=qSqrt(qPow(xx- pt_5.x(),2)+qPow(yy- pt_5.y(),2));
distance_pt_6=qSqrt(qPow(xx- pt_6.x(),2)+qPow(yy- pt_6.y(),2));

if (distance_pt_1 < min_distance)
{
min_distance= distance_pt_1;

}
else if (distance_pt_2 < min_distance)
{
min_distance= distance_pt_2;

}
else if (distance_pt_3 < min_distance)
{
min_distance= distance_pt_3;

}
else if (distance_pt_4 < min_distance)
{
min_distance= distance_pt_4;

}
else if (distance_pt_5 < min_distance)
{
min_distance= distance_pt_5;

}
else if (distance_pt_6 < min_distance)
{
min_distance= distance_pt_6;

}



if (min_distance== distance_pt_1 ){
char_flag=char_flag.append("1");
}
else if (min_distance== distance_pt_2 ){
char_flag=char_flag.append("2");
}
else if (min_distance== distance_pt_3 ){
char_flag=char_flag.append("3");
}
else if (min_distance== distance_pt_4 ){
char_flag=char_flag.append("4");
}
else if (min_distance== distance_pt_5 ){
char_flag=char_flag.append("5");
}
else if (min_distance== distance_pt_6 ){
char_flag=char_flag.append("6");
}

qDebug()<<"flag " <<char_flag;

} // foreach loop

最佳答案

由于您使用的是 if - else if,因此并未检查您的所有条件。这意味着第一个条件始终为真(distance_pt_1 小于初始的 min_distance),但永远不会评估下一个条件。只需删除 else 子句即可。

if (distance_pt_1 < min_distance) {
min_distance = distance_pt_1;
}
if (distance_pt_2 < min_distance) {
min_distance = distance_pt_2;
}
if (distance_pt_3 < min_distance) {
min_distance = distance_pt_3;
}
if (distance_pt_4 < min_distance) {
min_distance = distance_pt_4;
}
if (distance_pt_5 < min_distance) {
min_distance = distance_pt_5;
}
if (distance_pt_6 < min_distance) {
min_distance = distance_pt_6;
}

关于c++ - 给Qt中距离最小的点一个标志的语义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27541478/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com