import javax.swing.*;

public class Catenary {

	public static void main(String[] args) {
		String input;

		// Prompt and read weight, heights, locations from user		
		input = JOptionPane.showInputDialog("Enter cable weight (N/m)");
		double w = Double.parseDouble(input);
		input = JOptionPane.showInputDialog("Enter min height(m)");
		double yMin = Double.parseDouble(input);
		input = JOptionPane.showInputDialog("Enter pole height(m)");
		double y = Double.parseDouble(input);
		input = JOptionPane.showInputDialog("Enter pole location(m)");
		double x = Double.parseDouble(input);

		// Solve for tension T
		double Tmin= w;
		double Tmax= w*x*y;

		final double epsilon= 0.0001;
		double T= bisect(Tmin, Tmax, epsilon, x, y, yMin, w);
		System.out.println("Tension: "+ T);
		System.out.println("Cable weight w: "+w);
		System.out.println("Min cable height yMin: "+yMin);
		
		// Print out y value for x= 0 to pole location
		printHeight(yMin, T, w, x);
		System.exit(0);
	}
	public static void printHeight(double yMin, double T, double w, double xMax) {
		long nbrIterations= Math.round(xMax);
		System.out.println("\nCable height");
		for (int i=0; i <= nbrIterations; i++) {
			double x= i;
			double y= T/w*cosh(w*x/T) + yMin - T/w;
			System.out.println((double)i + " "+y);
	}
	}
	
	public static double f(double x, double y, double yMin, double T, double w) {
		double answer= y - T/w*cosh(w*x/T) - yMin + T/w;
		return answer;	
	}
	
	public static double cosh(double x) {
		//double answer = 0.5*(Math.exp(x) + Math.exp(-x));
	    double answer = Math.cosh(x);
		return answer;
	}
	
	public static double bisect(double T1, double T2, double epsilon, double x,
		double y, double yMin, double w) {
		double m;
		for (m= (T1+T2)/2.0; Math.abs(T1-T2) > epsilon; m= (T1+T2)/2.0)
			if (f(x, y, yMin, T1, w)*f(x, y, yMin, m, w) <= 0.0)
			// If func crosses zero in left sub
				T2= m;       // Use left subinterval
			else
				T1= m;       // Use right subinterval
			return m;
		}	
}