gpt4 book ai didi

c++ - Square 无法正常工作 C++ Xcode 4.2.1

转载 作者:行者123 更新时间:2023-11-27 23:19:09 25 4
gpt4 key购买 nike

<分区>

我正在使用 xcode v. 4.5.1 并尝试使用 C++ 项目的距离公式 (sqrt( (x1-x2)^2 + (y1-y2)^2 )) 计算两点之间的距离但出于某种原因,我的值(value)观没有被平方。为什么会发生这种情况,我如何计算一个值的平方?

source 这里是点对象的 vector 。我正在使用的 vector 是 (3,4) (6,8) (50,8) (11,7)。如果我打印 source[i] , source[i+1]source[j].x().y()最后,它们都适本地打印出来,当我打印出来时 source[i].x()-source[i+1].x()等值是正确的。但是一旦我添加了“^2”,打印的值都是错误的。

例如,

cout<< source[i].y();  shows 4 8 8 7 (when looped over i)

但是

cout<< sqrt((source[i].x()-source[i+1].x())^2 + (source[i].y()-source[i+1].y())^2);

当 i=1 时显示 1

这是我的包括:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include "Point.h"

这里是代码的一部分,不会为我计算平方值:

void function(vector<Point>& source){
if(source.size()>3){
for(int i=0; i<n+2; i++){
for(int j=i+2; j<n; j++){
double d1 = sqrt((source[i].x()-source[i+1].x())^2 + (source[i].y()-source[i+1].y())^2);
double d2 = sqrt((source[i].x()-source[j].x())^2 + (source[i].y()-source[j].y())^2);
if( d1 > d2){
swap( source[i+1], source[j] );
}
}
}
}

25 4 0
文章推荐: javascript - 如何解决从带有网格的 scrollmagic 生成的
文章推荐: html - 背景样式
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com