phobos
phobos

Reputation: 105

How to find the intersection of two curves in Scilab?

I have two curves (non linear) say C1 and C2. which intersect with each other(can be more than once). I need to find these points of intersection using Scilab only.

Upvotes: 0

Views: 3052

Answers (3)

Ishwar Maharudrappa
Ishwar Maharudrappa

Reputation: 1

Step1 Type "xclick" in console and then tap enter step2 now go to your graphics window Step3.click on point where the curves C1 and C2 touch each other. "you can able to get the point coordinates(output_point of intersection) in the console"

Upvotes: 0

Rosestock
Rosestock

Reputation: 9

Hallo Scilab and Matlab community,

Although this is an ancient thread I will post an answer. Intersection of two curves given by interpolation points is a standard problem. I had to solve it for a particular application, found many useful indoctrinations in the web but no viable solution. Below it comes as the function X_Crossing applied to a non trivial situation invented for this thread (a bunch of intersections between a Lissajous line and two ellipses separated by NaNs.) For convenience copy and paste into the Scilab console should suffice to present the result.

Good luck

Rosestock

Sorry, I did notunderstand your code block instructions. And sorry, I did not find the preview button.

clear
clc()
mode(0)
format(10)


function [xC,yC,nC,dxC,dyC,duC,dvC]=X_Crossing(x,y,u,v,d_near,info)
  //Alle Schnittpunkte zweier Polygonzüge (offen oder geschlossen)) 
  //Selbstüberschneidungen sind keine Schnittpunkte!
  //INPUT
  //x.y. u,v: Koordinaten der beiden Kurvwen, 1 x nxy bzw. 1 x nuv
  //Wenn möglich, alle Polygonkanten etwa gleich lang.
  //d_near: Schnittpunkte werden nur in den Bereichen der Kurven 
  //        gesucht, in denen die Kurven sich näher als d_near sind.
  //        d_near zu klein: Schnittpunkte werden übersehen. 
  //        d_near unnötig groß: Rechnung dauert unnötig lange. 
  //        Erster Versuch: d_near ca. dreifacher Punktabstand
  //OUTPUT
  //xC,yC: Koordinaten der Scnittpunkte, Zeilenvektoren
  //nC: Anzahl der gefundenen Schnittpunkte
  //info: 1: Hinweise, 0: keine Hinweise
  //METHODE
  // Scnittpunkte der Kantengeraden 

  warning('off')
  Meldung=[' ']//Initialisierung
  dxC=[];
  dyC=[];
  duC=[];
  dvC=[];
  nC=0//Initialisierung Anzahl Schnittpunkte
  nxy=length(x)
  nuv=length(u)
  //Abschnitte in denen die Kurven nahe verlaufen
  I=[];
  for j=1:nuv
    //Abstand des j-ten W-Punktes von SFK
    d=sqrt([u(j)-x].^2+[v(j)-y].^2);
    i_near=find(d<d_near);
    I=[I i_near];//Indizes einer Untermenge von x,y
  end//for j=1:nuv      
  I=unique(gsort(I));
  I=I';
  nI=length(I)  
  diffIgt1=find(diff(I)>1)';

  Ia=[I(1); I(diffIgt1(1:$)+1)];
  Ie=[I(diffIgt1(1:$));I($)];     
  nNahbereich=length(Ie);
  //printf('\n nI: %f \n',nI)
  //printf('\n Ia: %f \n',Ia)
  //printf('\nnNahbereich: %f \n',nNahbereich)

  if nNahbereich==0 then
     if info
       infotext=['Der Kurvenabstand ist überall > d_near.';
                'Wenn Schnittpunkte existieren, d_near vergrößern!'];
       Meldung=[Meldung;infotext]
       printf('\n\n%4.0f Schnittpunkte gefunden.\n',nC)
       printf('%s\n',Meldung)
     end//if info
     xC=%nan;
     yC=%nan;
     return
  end// if nNahbereich= == 0 then

  xC=[];//Schittpunktkoordinaten
  yC=[];//Schittpunktkoordinaten
  for k=1:nNahbereich
  xNah=x(Ia(k):Ie(k));
  yNah=y(Ia(k):Ie(k));

  for i=1:length(xNah)-1
    for j=1:nuv-1
     dx=xNah(i+1)-xNah(i);
     dy=yNah(i+1)-yNah(i);
     du=-u(j+1)+u(j);
     dv=-v(j+1)+v(j);
     dxy=[dx;dy];
     duv=[du;dv];
     if ~isnan(dx)&~isnan(dy)&~isnan(du)&~isnan(dv)..
         & dx*dv~=dy*du// nicht parallel  
         M=[dxy duv];//2 x 2
         t=M\[u(j)-xNah(i);v(j)-yNah(i)];
       if t(1)>=0 & t(1)<1 & t(2)>=0 & t(2)<1
         xyC=[xNah(i);yNah(i)]+t(1)*dxy;
         xC=[xC xyC(1)];
         yC=[yC xyC(2)];
         dxC=[dxC dx]
         dyC=[dyC dy]
         duC=[duC du]
         dvC=[dvC dv]
       end//if t(1)>=0 & ...
     end//if ~isnan(dx)& ...
   end//for j=1:nuv-1
  end//for i=1:nxy-1
  if length(xC)>0;
    nC=length(xC)
//      browsevar()
//      abort
    if info
      printf('\n %5.0f Schnittpunkte gefunden',nC)
    end//if info
  end//if length(xC)>0;
  if isempty(xC)|isempty(yC)
    if info
       infotext=['Wenn mehr existieren, d_near vergrößern!'];
       Meldung=[Meldung; infotext]
       printf('\n\n%4.0f Schnittpunkte gefunden.\n',nC)
       printf('%s\n',Meldung)
    end//if info
     xC=%nan;
     yC=%nan;
    return 
  end//if isempty(xC)|isempty(yC)
end//for k= 
endfunction//function [xC,yC,I,J]=X_Crossing(x,y,u,v)

if 1//just for stack overflow's forum parser
   phiL=linspace(0,360,300);//Winkel für Lissajous-Figur
   phiE=linspace(0,360,60);//Winkel für Ellipsen
   //Zwei Ellipsen
   x=-0.05+0.9*cosd(phiE);
   x=[x %nan 0.5*x+0.5];
   y=0.3+0.6*sind(phiE);
   y=[y %nan 0.5*y-0.8];
   u=cosd(3*phiL);
   v=sind(5*phiL);
   d_near=0.28;
   info=1;
   printf('\nBitte warten!\n')
   printf(getversion()+' sucht Schnittpukte...')
   xdel()
   fig=figure('position',[450 -50 700 700],'background',8,'visible','off');
   [xC,yC,nC,dxC,dyC,duC,dvC]=X_Crossing(u,v,x,y,d_near,info);
   if ~isnan(xC)|~isnan(yC)
     plot(xC,yC,'or')//Schnittpunktmarker
   end//if ~isnan(xC)&~isnan(yC)
   plot(x,y,u,v)//
    //   //Schnittpunkt-Tangenten
    //   plot([xC;xC+dxC], [yC;yC+dyC],'r','thickness',2)
    //   plot([xC;xC+duC], [yC;yC+dvC],'r','thickness',2)
   ax=gca();
   ax.isoview='on';
   set(fig,'visible','on')
    // browsevar()
    end//if 1//just for stack overflow's forum parser

Upvotes: 0

jofafrazze
jofafrazze

Reputation: 143

There's two examples of this in the an Scilab tutorial over at Open Source Engineering:

Solving Nonlinear Systems in Scilab [PDF].

Upvotes: 1

Related Questions