function [x_intersect y_intersect]=SOLEIL_angles_intersect(x1,y1,x2,y2)
	%detector_grating_separation=@(c,a1,a2)(c*sin(a2*pi/180)/sin(a1*pi/180)/sin((a2-a1)*pi/180));
	%h=zeros(length(positions)-1,1);
	%for i_counter=1:length(positions)-1
	%	h(i_counter,1)=detector_grating_separation(positions(i_counter)-positions(i_counter+1),...
		%90-angles(i_counter+1),90-angles(i_counter));
	%end
	%mean_h = mean(h)
	%rms_h = rms(h-mean_h);
	%x_0=mean(positions+tan((angles)*pi/180).*mean_h)

	%line1
	%x1  = [positions(1) positions(1)+20*sin(angles(1)*pi/180)];
	%y1  = [0 20*cos(angles(1)*pi/180)];
	%line2
	%x2 = [positions(2) positions(2)+20*sin(angles(2)*pi/180)];
	%y2 = [0 20*cos(angles(2)*pi/180)];

	%fit linear polynomial
	
	if ((diff(x1-x2)==0) && (diff(y1-y2)==0)) % if lines are parallel, Inf
	 x_intersect=1/0;			
	 y_intersect=1/0;
	 return
	end; % if parallel

	if ((abs(diff(x1))<(abs(min(x1))*10e-12))||...
	   ((abs(diff(x2))<(abs(min(x2))*10e-12)))) % if there is vertical line
		p1 = polyfit(y1,x1,1);
        	p2 = polyfit(y2,x2,1);
        	%calculate intersection
        	y_intersect = fzero(@(x) polyval(p1-p2,x),3);
	        x_intersect = polyval(p1,y_intersect);
	else
		p1 = polyfit(x1,y1,1);
		p2 = polyfit(x2,y2,1);
		%calculate intersection
		x_intersect = fzero(@(x) polyval(p1-p2,x),3);
		y_intersect = polyval(p1,x_intersect);
	end %vertical line
end
