Graphics in C Language

The shortest line between two lines in 3D

Two lines in 3 dimensions generally don’t intersect at a point, they may be parallel (no intersections) or they may be coincident (infinite intersections) but most often only their projection onto a plane intersect.. When they don’t exactly intersect at a point they can be connected by a line segment, the shortest line segment is unique and is often considered to be their intersection in 3D.

The following will show how to compute this shortest line segment that joins two lines in 3D, it will as a biproduct identify parrallel lines. In what follows a line will be defined by two points lying on it, a point on line “a” defined by points P1 and P2 has an equation

Pa = P1 + mua (P2
P1)

similarly a point on a second line “b” defined by points
P4 and P4 will be written as

Pb = P3 + mub
(P4 – P3)

The values of mua and mub range from negative to positive infinity. The line segments between P1
P2 and P3 P4 have their corresponding mu
between 0 and 1.

There are two approaches to finding the shortest line segment between lines “a” and “b”. The first is to write down the length of the line segment joining the two lines and then find the minimum. That is, minimize the following

|| Pb – Pa ||2

Substituting the equations of the lines gives

|| P1 – P3 + mua (P2
P1) – mub (P4 – P3) ||2

The above can then be expanded out in the (x,y,z) components. There are conditions to be met at the minimum, the derivative with respect to mua and mub must be zero. Note: it is easy to convince
oneself that the above function only has one minima and no other minima or maxima. These two equations can then be solved for mua and mub, the actual intersection points found by substituting the values of mu into the original equations of the line.

An alternative approach but one that gives the exact same equations is to realize that the shortest line segment between the two lines will be perpendicular to the two lines. This allows us to write two equations for the dot product as

(Pa – Pb) dot (P2 – P1) = 0

(Pa – Pb) dot (P4 – P3) = 0

Expanding these given the equation of the lines

( P1 – P3 + mua (P2
P1) – mub (P4 – P3) ) dot
(P2 – P1) = 0

( P1 – P3 + mua (P2
P1) – mub (P4 – P3) ) dot
(P4 – P3) = 0

Expanding these in terms of the coordinates (x,y,z) is a nightmare but the

result is as follows

d1321 + mua d2121 – mub
d4321 = 0

d1343 + mua d4321 – mub
d4343 = 0

where

dmnop = (xm – xn)(xo
xp) + (ym – yn)(yo – yp)
+ (zm – zn)(zo – zp)

Note that dmnop = dopmn

Finally, solving for mua gives

mua = ( d1343 d4321 – d1321
d4343 ) / ( d2121 d4343 – d4321
d4321 )

and backsubstituting gives mub

mub = ( d1343 + mua d4321 )
/ d4343

146 Comments

  1. I need to make a programming that works like a voting system…compute votes…and calculates score for finalist and display them in a data text file…using classes and object can anyone help me…

    • Check it out…….

      #include
      using namespace std;
      class voting
      {
      int count[5];
      int spoilt;
      public:
      void initial()
      {
      for(int i=0; i<5; i++)
      {
      count[i]=0;
      }
      spoilt=0;
      }
      void vote(int v)
      {
      if(v0)
      {
      if(v==1)
      count[0]++;
      else if(v==2)
      count[1]++;
      else if(v==3)
      count[2]++;
      else if(v==4)
      count[3]++;
      else
      count[4]++;
      }
      else
      spoilt++;
      }
      void dispvote()
      {
      int i;
      for(i=0; i<5; i++)
      {
      cout<<"candidate"<<i+1<<":"<<count[i]<<endl;
      }

      }
      void dispspoilt()
      {
      cout<<"total spoilt votes:"<<spoilt<<endl;
      }
      };
      int main()
      {
      voting v1;
      v1.initial();
      int choice;
      do
      {
      cout<>choice;
      switch(choice)
      {
      case 1:
      int v2;
      cout<>v2;
      v1.vote(v2);
      break;
      case 2:
      v1.dispvote();
      break;
      case 3:
      v1.dispspoilt();
      break;
      }
      }
      while(choice<4);
      return 0;
      }

  2. //This is a game cross and nought( Zero Katta) in c++.

    void show_game_box();
    char m[3][3];
    void main()
    {
    	int p,q;
    	char ans;
    	cout<<"\t\tCROSS & NOUGHT GAME\n";
    	do
    	{
    		for(p=1;p<=3;p++)
    		{
    			for(q=1;q<=3;q++)
    			{
    				m[p][q]='';
    			}
    		}
    		int i,j,sum=0;
    		while(sum<10)
    		{
    		if(sum==0)
    		show_game_box();
    		cout<<"\n\n\nPlayer 1 is'0'\n";
    		cout<<"Player 1's turn\n";
    		cout<>i;
    		cout<>j;
    		for(;(i>3)||(j<1)||('X'==m[i][j])||('0'==m[i][j]);)
    		{
    			cout<<"Sorry you entered wrong choice\n";
    			cout<<"Enter your choice again\n";
    			cout<>i;
    			cout<>j;
    		}
    		m[i][j]='0';
    		sum++;
    		show_game_box();
    		if((m[1][1]=='0')&&(m[1][1]==m[1][2])&&(m[1][1]==m[1][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[2][1]=='0')&&(m[2][1]==m[2][2])&&(m[2][1]==m[2][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[3][1]=='0')&&(m[3][1]==m[3][2])&&(m[3][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[1][1]=='0')&&(m[1][1]==m[2][2])&&(m[1][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[1][3]=='0')&&(m[1][3]==m[2][2])&&(m[1][1]==m[3][1]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[1][1]=='0')&&(m[1][1]==m[2][1])&&(m[1][1]==m[3][1]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[1][2]=='0')&&(m[1][2]==m[2][2])&&(m[1][2]==m[3][2]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if((m[1][3]=='0')&&(m[1][3]==m[2][3])&&(m[1][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 1 wins the game\n";
    			break;
    		}
    		if(sum==9)
    		{
    			cout<<"\t\tHURRAY!!!!! The game is over\n";
    			cout<<"\t\tNO ONE WINS\n";
    			cout<<"The game is draw\n";
    			break;
    		}
    		cout<<"\n\n\nPlayer 2 is'X'\n";
    		cout<<"Player 2's turn\n";
    		cout<>i;
    		cout<>j;
    		for(;(i>3)||(j<1)||('X'==m[i][j])||('0'==m[i][j]);)
    		{
    			cout<<"Sorry you entered wrong choice\n";
    			cout<<"Enter your choice again\n";
    			cout<>i;
    			cout<>j;
    		}
    		m[i][j]='X';
    		sum++;
    		show_game_box();
    		if((m[1][1]=='X')&&(m[1][1]==m[1][2])&&(m[1][1]==m[1][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[2][1]=='X')&&(m[2][1]==m[2][2])&&(m[2][1]==m[2][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[3][1]=='X')&&(m[3][1]==m[3][2])&&(m[3][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[1][1]=='X')&&(m[1][1]==m[2][2])&&(m[1][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[1][3]=='X')&&(m[1][3]==m[2][2])&&(m[1][1]==m[3][1]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[1][1]=='X')&&(m[1][1]==m[2][1])&&(m[1][1]==m[3][1]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[1][2]=='X')&&(m[1][2]==m[2][2])&&(m[1][2]==m[3][2]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if((m[1][3]=='X')&&(m[1][3]==m[2][3])&&(m[1][1]==m[3][3]))
    		{
    			cout<<"CONGRATULATIONS!!!!!!!!!!\n";
    			cout<<"player 2 wins the game\n";
    			break;
    		}
    		if(sum==9)
    		{
    			cout<<"\t\tHURRAY The game is over\n";
    			cout<<"\t\tNO ONE WINS\n";
    			cout<<"\t\tThe game is drawn\n";
    			break;
    		}
    		}
    		cout<>ans;
    	}while((ans=='y')||(ans=='Y'));
    	system("PAUSE");
    	getch();
    }
    void show_game_box()
    {
    	cout<<"\n    1 2 3\n"<<endl;
    	cout<<"  1 "<<m[1][1]<<"|"<<m[1][2]<<"|"<<m[1][3]<<endl;
    	cout<<"    -|-|-\n";
    	cout<<"  2 "<<m[2][1]<<"|"<<m[2][2]<<"|"<<m[2][3]<<endl;
    	cout<<"    -|-|-\n";
    	cout<<"  3 "<<m[3][1]<<"|"<<m[3][2]<<"|"<<m[3][3]<<"\n\n\n";
    }
    • took your cursur to the header file in the c program and then press
      “ctrl+f1” then u found all the functions of that header file

  3. could anyone help me to get a code for conversion of a digital image file into the matrix containing the pixels values of the image..

  4. can u give a tutorial about this.

    Project Name: Implementation of Recursion and Tracing its Stack (Graphically)

    Description: This project will implement and trace recursive functions graphically.

  5. i using a turbo c++ to do a graphical in C..this error be occur what should i do…
    Fatal ..\INCLUDE\GRAPHICS.H 19: Error directive: BGI graphics not supported under Windows

  6. i using a tc3 to do a graphical in C..this error be occur what should i do…

    #include <graphics.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    
    void draw(int x1,int y1,int x2,int y2);
    void main()
    {
    
        int x1,y1,x2,y2;
        int gdriver=DETECT,gmode,errorcode;
        initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
        printf("Enter the first point \n");
        scanf("%d%d",&x1,&y1);
        printf("\n\n Enter the second point");
        scanf("%d%d",&x2,&y2);
        printf("\n\n The line is shown below");
        draw(x1,y1,x2,y2);
        getch();
    }
    void draw(int x1,int y1,int x2,int y2)
    {
      int x,y,e,i,dx,dy,a;
      x=x1;
      y=y1;
      dx=x2-x1;
      dy=y2-y1;
      a=dx;
      dx=dy;
      dy=a;
      e=2*dy-dx;
      for(i=1;i0)
        {
          x=x+1;
          e=e-2*dx;
        }
        y=y+1;
        e=e+2*dy;
      }
      getch();
    }
    when i run this programme getting error
    Linker error:undefined symbol  _putpixel in module myfile name.
    i have executed this programme in lab pc.
  7. hello…
    need ur help guyz..
    i have to add graphics in ma game hangman… plz help me… i dun even knw how to start..:(
    i av to submitt ma project tomorrow… culd anyone help me plz..

    nd how to add a library iostream….??

Leave a Reply