Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Finding equation of a line

By: a guest on Jan 23rd, 2013  |  syntax: C  |  size: 2.42 KB  |  views: 119  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. #include "stdio.h"
  4.  
  5. float slope(float rise, float run)
  6. {
  7.         /*declaring variables*/
  8.         int x1;
  9.         int y1;
  10.         int x2;
  11.         int y2;
  12.  
  13.         /*equation to find slope of line, where m=slope*/
  14.         rise=y2-y1;
  15.         run=x2-x1;
  16.         return rise/run;
  17. }
  18.  
  19. float intercept(float y1, float slope, float x1)
  20. {
  21.         /*equation of the line*/
  22.         return y1-(slope*x1);
  23. }
  24.  
  25. int y_coordinate(float slope, float x, float intercept)
  26. {
  27.         /*equation to find the y coordinate*/
  28.         return slope*x+intercept;
  29. }
  30.  
  31. int x_coordinate(float y, float intercept, float slope)
  32. {
  33.         /*equation to find the x coordinate*/
  34.         return (y-intercept)/slope;
  35. }
  36.  
  37. int main()
  38. {
  39.         /*declaring variables*/
  40.         int x1;
  41.         int y1;
  42.         int x2;
  43.         int y2;
  44.         float x;
  45.         float y;
  46.         float intercept;
  47.         float slope;
  48.         float rise;
  49.         float run;
  50.         int a = 1;
  51.  
  52.         while (a != 0)
  53.         {
  54.  
  55.                 /*user input*/
  56.                 printf("x1: ");
  57.                 scanf("%d", &x1);
  58.                 printf("y1: ");
  59.                        scanf("%d", &y1);
  60.                        printf("x2: ");
  61.                 scanf("%d", &x2);
  62.                 printf("y2: ");
  63.                 scanf("%d", &y2);
  64.  
  65.                 /*using previous functions to calculate the equation of the line*/
  66.                 y = slope(rise, run)*x + intercept(y1, slope, x1);
  67.  
  68.                 /*print equation of the line*/
  69.                        printf("y = %fx + %f\n", slope, intercept);
  70.  
  71.                 /*user input for x value*/
  72.                 printf("Enter a value for x: ");
  73.                 scanf("%f", &x);
  74.  
  75.                 /*using previous function to find y-intercept*/
  76.                 y= y_coordinate(slope, x, intercept);
  77.  
  78.                 /*print y coordinate*/
  79.                 printf("y = %f\n", y);
  80.  
  81.                 /*user input for y value*/
  82.                 printf("Enter a value for y: ");
  83.                 scanf("%f", &y);
  84.  
  85.                 /*using previous function to find x-intercept*/
  86.                 x= x_coordinate(y, intercept, slope);
  87.  
  88.                 /*print x cooridinate*/
  89.                 printf("x = %f\n", x);
  90.  
  91.                 /*user input to decide whether or not to continue*/
  92.                 printf("Enter 0 to quit, 1 to continue: ");
  93.                 scanf("%d", &a);
  94.         }
  95.  
  96.         return 0;
  97. }