Example of Distance from Min Max Rotation Value

Hello everyone!

Here is get_3d_distance_from_min_max function to get distance similar to get_3d_distance function in rotation.nvgt but this is base from min max rather than just 1 base coordinate.

Please leave me out whether or not this function should be added to rotation.nvgt.

double get_3d_distance_from_min_max(vector current, vector min, vector max) {
	double dx = (current.x < min.x) ? (min.x - current.x) : (current.x > max.x) ? (current.x - max.x) : 0;
	double dy = (current.y < min.y) ? (min.y - current.y) : (current.y > max.y) ? (current.y - max.y) : 0;
	double dz = (current.z < min.z) ? (min.z - current.z) : (current.z > max.z) ? (current.z - max.z) : 0;
	return sqrt(dx * dx + dy * dy + dz * dz);
}

Also, x_y_angle.

shared double calculate_x_y_angle_from_min_max(vector current, vector min, vector max, double deg, bool at_least_1_tile = true, bool floor_deg = true) {
	// Clamp current.x to be within the range [min.x, max.x]
	double clamped_x = (current.x < min.x) ? min.x : (current.x > max.x) ? max.x : current.x;
	
	// Clamp current.y to be within the range [min.y, max.y]
	double clamped_y = (current.y < min.y) ? min.y : (current.y > max.y) ? max.y : current.y;
	
	// Calculate 2D distance between the current point and the clamped point
	if (at_least_1_tile && get_2d_distance(current.x, current.y, clamped_x, clamped_y) < 1)
		return 0;
	
	// Calculate x and y differences between current and clamped point
	double x = clamped_x - current.x;
	double y = clamped_y - current.y;

	// Check if there’s any distance to compute an angle
	if (x == 0 && y == 0)
		return 0;

	// Calculate the angle in radians, convert to degrees, and adjust by `deg`
	double rad = atan2(x, y);
	double fdeg = rad * (180.0 / pi);
	fdeg -= deg;

	// Normalize the angle to be within 0 to 360 degrees
	while (fdeg < 0)
		fdeg += 360;

	// Apply flooring if specified
	if (floor_deg)
		fdeg = floor(fdeg);

	return fdeg;
}
1 Like