简体   繁体   中英

How can i find the length of a line between two points with coordinates (x1, y1) and (x2, y2) in array?

How can i find the length of a line between two points with coordinates (x1, y1) and (x2, y2) in array? For example i have 2 arrays: arrayX[10] and arrayY[10].

I have to find the results of the following operations in the loop and save them in the result array:

sqrt((arrayX[0]- arrayX[1])^2+(arrayY[0]-arrayY[1]^2))

sqrt((arrayX[1]- arrayX[2])^2+(arrayY[1]-arrayY[2]^2))

.

.

.

In C there is no exponentiation operator. To compute x^2 either use x * x or pow(x, 2) from math.h .

Possible solution

double dist[9];
for (int i = 0; i + 1 < 10; ++i) {
   double dx = arrayX[i]- arrayX[i + 1];
   double dy = arrayY[i]- arrayY[i + 1];
   dist[i] = sqrt(dx *dx + dy * dy);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM