Error in calculation rotation of static mesh for z axis

I’m implementing a zipline component with two poles and a cable(static mesh) between them. In the OmCostruction function, I automatically scale and rotate the cable so that it is located between the two top points of the post. However, something strange happens when turning along the z axis
code:

void AZipline::OnConstruction(const FTransform& Transform)
{
FVector StartLocation = RightRailMeshComponent->GetSocketLocation(FName(“AttachPoint”));
FVector EndLocation = LeftRailMeshComponent->GetSocketLocation(FName(“AttachPoint”));
float Delta = sqrt(pow(StartLocation.X - EndLocation.X, 2) + pow(StartLocation.Y - EndLocation.Y, 2) + pow(StartLocation.Z - EndLocation.Z, 2));
float Length = sqrt(pow(StartLocation.X - EndLocation.X, 2) + pow(StartLocation.Y - EndLocation.Y, 2) + pow(StartLocation.Z - EndLocation.Z, 2));
float a = sqrt(pow(StartLocation.X,2)+ pow(StartLocation.Y, 2)+ pow(StartLocation.Z, 2) );
if (IsValid(ZiplineMeshComponent) && IsValid(ZiplineMeshComponent->GetStaticMesh())) {
float MeshHeight = ZiplineMeshComponent->GetStaticMesh()->GetBoundingBox().GetSize().Z;
float X = (StartLocation.X + EndLocation.X) / 2;
float Y = (StartLocation.Y + EndLocation.Y) / 2;
float Z = (StartLocation.Z + EndLocation.Z) / 2;
float DeltaX = -StartLocation.X + EndLocation.X;
float DeltaY = -StartLocation.Y + EndLocation.Y;
float DeltaZ = -StartLocation.Z + EndLocation.Z;
float cosX = (DeltaX / Delta);
float cosY = (DeltaY / Delta);
float cosZ = (DeltaZ / Delta);

	double AlfaX = acos(cosX)*180/3.1415;
	if (AlfaX > ZiplineMeshComponent->GetRelativeRotation().Vector().X)
		AlfaX *= -1;
	double AlfaY = acos(cosY) * 180 / 3.1415;
	if (AlfaY > ZiplineMeshComponent->GetRelativeRotation().Vector().Y)
		AlfaY *= -1;
	double AlfaZ = acos(cosZ) * 180 / 3.1415;
	if (AlfaZ < ZiplineMeshComponent->GetRelativeRotation().Vector().Z)
		AlfaZ *= -1;
	ZiplineMeshComponent->SetWorldLocation(FVector(X, Y, Z));
	ZiplineMeshComponent->SetRelativeRotation(FRotator(AlfaX,AlfaY,AlfaZ));
	ZiplineMeshComponent->SetRelativeScale3D(FVector(1.0f, 1.0f, Length / MeshHeight));
}
if (IsValid(InteractionVolume)) {
	GetZiplineInteractionCapsule()->SetCapsuleHalfHeight(Length/2);
	InteractionVolume->SetWorldLocation(ZiplineMeshComponent->GetComponentLocation());
}

}