procedure(calculer_intersection_segment(x1 y1 x2 y2 x3 y3 x4 y4)
;(2 1 10 8 1 9 7 3)	
	prog( ()
		let((denominateur numerateur_ua numerateur_ub ua ub x y point_intersection)
  
		x1=float(x1)	        
		y1=float(y1)
		x2=float(x2)
		y2=float(y2)
		x3=float(x3)	        
		y3=float(y3)
		x4=float(x4)
		y4=float(y4)

		;si le dÃƒÆ’Ã‚Â©nominateur est commun et que si celui-ci est nul alors les deux segments sont parallÃƒÆ’Ã‚Â¨les
		;si le dÃƒÆ’Ã‚Â©nominateur et les numÃƒÆ’Ã‚Â©rateurs sont nuls alors les deux segments sont colinÃƒÆ’Ã‚Â©aires.

		ua_numerateur=(x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
		;printf("ua_numerateur %L \n" ua_numerateur)
		ub_numerateur=(x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
		;printf("ub_numerateur %L \n" ub_numerateur)
		denominateur=(y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
		;printf("denominateur %L \n" denominateur)

		if(denominateur!=0.0 then
			;printf("different de zero \n")
			ua=ua_numerateur/denominateur
			;printf("ua %L \n" ua)
			ub=ub_numerateur/denominateur
			;printf("ub %L \n" ub)

			if(ua>=0.0 && ua<=1.0 && ub>=0.0 && ub<=1.0 then
			x=x1+ua*(x2-x1)
			;printf("x %L \n" x)
			y=y1+ua*(y2-y1)
			;printf("y %L \n" y)
			
	     		point_intersection=list(x y)       
	        	return(point_intersection)

			);if

		);if
		
	      	
	);let
	);prog
);procedure   
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(minimum(liste)

   prog( ()

		for(j 0 length(liste)-1
	        	if(j == 0 then  
	 		   a = nth(j liste)
			   b = nth(j+1 liste)
			   val = min(a b)
			)
			if(j > 0 then  
			   a = nth(j liste)
			   val = min(val a)
			)
	   	)

		return(val) 

	)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(maximum(liste)

   prog( ()

		for(j 0 length(liste)-1
	        	if(j == 0 then  
	 		   a = nth(j liste)
			   b = nth(j+1 liste)
			   val = max(a b)
			)
			if(j > 0 then  
			   a = nth(j liste)
			   val = max(val a)
			)
	   	)

		return(val) 

	)
) 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(clean()   


	cv = geGetEditCellView() 
	win_id = getCurrentWindow() 

	buffer_main = '()

	buffer_main = geGetSelSet()

	for(i 0 length(buffer_main)- 1 
	    id = nth(i buffer_main)
	    id~>pathStyle="extendExtend"
	)

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure(ppp()   

	segments = '()
	segments = geGetSelSet()	
	coord_concatener = '()
	id_path_detecter = 0

	printf(">>>> STEP1 \n")

	for(i 0 length(segments)-1
	    points='() 
	    id = nth(i segments)
	    points = id~>points
	    nb_points = id~>nPoints
    	    for(j 0 nb_points - 1
	       coord_concatener = append1(coord_concatener nth(j points ))
	    )
	)

	printf(">>>> STEP2 \n")

	liste_x = '()
	liste_y = '()

	for(i 0 length(coord_concatener)-1
		x = car(nth(i coord_concatener))
		liste_x = append1(liste_x x )
		y = cadr(nth(i coord_concatener))
		liste_y = append1(liste_y y )

	)

	mini_x = minimum(liste_x)

	stop = nil
	
	for(i 0 length(segments)-1
	    for(j 0 length(liste_y) - 1
		 if(stop == nil then	
		    p = list(mini_x nth(j liste_y))
		    printf("---- %L \n" p)
		    test = leIsPointInsideFig(nth(i segments) p )   
		    if(test == t then
		       id_path_detecter = nth(i segments)
		       printf("**** %L \n" id_path_detecter) 	
		       mini_y = nth(j liste_y)
		       println("bingo")
		       stop = t
		    )
		 )
	     )
	)

	printf(">>>> STEP3 \n")


	points = '()
	points = id_path_detecter~>points
	liste_y = '()

	for(i 0 length(points) - 1
	    y = cadr(nth(i points))
	    liste_y = append1(liste_y y )
	)

	mini_y = minimum(liste_y)


	mini_x = 100.015
	mini_y = 22.21



	couple = '()
	point_depart = '()

	couple = car(id_path_detecter~>points)
	point_depart =  list(mini_x mini_y)
	printf("******* %L \n" point_depart)

	test =  equal(couple point_depart)
	if(test == nil then
	    ;; faire un reverse de la liste des points	
	    reverse_pts = '()
	    reverse_pts = reverse(id_path_detecter~>points)
	    couple1 = '()	
	    couple1 = car(reverse_pts)
	    id_path_detecter~>points = reverse_pts
	) 

	printf(">>>> STEP4 \n")


	liste_work = '()

	liste_work = remove(id_path_detecter segments  )
	id_path = id_path_detecter
	liste_ordre = '()
	liste_ordre = append1(liste_ordre id_path )
	
	i = 0
	flag = "no"
	stop = nil
	while( stop != t 
	       if( length(liste_work) == 0 then
		   stop = t
	       else	
	             test = leIsPointInsideFig( id_path car(nth(i liste_work)~>points)  )  
	             if(test == t then
			 printf(">>>> %L \n"  nth(i liste_work))
		         liste_ordre = append1(liste_ordre nth(i liste_work) )
		         liste_work = remove(nth(i liste_work) liste_work  )
		         id_path = nth(i liste_work)
			 flag = "ok" 
	             ) 
                     if(test == nil then
		        tmp = '()
		        tmp =  reverse(nth(i liste_work)~>points)
		        p = car(tmp)
		        test = leIsPointInsideFig( id_path p  )
	                if(test == t then
			   printf(">>>> %L \n"  nth(i liste_work))
			   nth(i liste_work)~>points = tmp
			   liste_ordre = append1(liste_ordre nth(i liste_work) )
			   liste_work = remove(nth(i liste_work) liste_work  )
			   id_path = nth(i liste_work)
			   flag = "ok" 
		        )
		     )
		
		     if(flag == "ok" then
		        i = 0
			else 
			    i = i + 1
			)
		
		

                )
	 ) 





)
  


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ppp()

;;(db:0xb7749dd0 db:0xb7749dce db:0xb7749dd1 db:0xb7749dcf db:0xb7749dd2)
