How to write a C program to Check three Points are in Straight Line

How to write a c program to check all the three points (x1,y1), (x2,y2

Page 1 of 1

3 Replies - 11025 Views - Last Post: 17 September 2009 - 11:34 PM Rate Topic: -----

#1 ausafahmads  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-September 09

How to write a C program to Check three Points are in Straight Line

Post icon  Posted 17 September 2009 - 11:15 PM

How to write a c program to check all the three points (x1,y1), (x2,y2) and (x3,y3) are in straight line..
Is This A Good Question/Topic? 0
  • +

Replies To: How to write a C program to Check three Points are in Straight Line

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to write a C program to Check three Points are in Straight Line

Posted 17 September 2009 - 11:23 PM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to write a C program to Check three Points are in Straight Line

Posted 17 September 2009 - 11:30 PM

in general --you do it just like you would in math.

take two of the points and make a foruma for a line,

y = ((y2 -y1)/(x2 -x1))*x3

if y == y3 then yes the three points are all on the same line.


there are more sophisticated methods such as using a matrix but this should work.

<edit> looking at aries0152 answer below I realize that mine is not correct... sorry its 2am here</edit>
Was This Post Helpful? 0
  • +
  • -

#4 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Re: How to write a C program to Check three Points are in Straight Line

Posted 17 September 2009 - 11:34 PM

View Postausafahmads, on 17 Sep, 2009 - 10:15 PM, said:

How to write a c program to check all the three points (x1,y1), (x2,y2) and (x3,y3) are in straight line..


Let the three points are P1(x1,y1), P2(x2,y2) and P3(x3,y3).They will be line in a straight line- If the gradients of P1P2 and P1P3 are same.

So try to write a program that calculates the values of gradient using (y1-y2)/(x1-x2) and (y1-y3)/(x1-x3) and compare the two functions. :)
Try to use if...else. If the values of these two functions are same then it gives the output something like "three points are in straight line" else "They are not in straight". Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1