Thread: function checks whether four points really define a square !!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    function checks whether four points really define a square !!

    Hi all, i started to do my task of programming, i need some help to understand and finish it
    i am writing the whole task so you could understand what i am talking about:

    " A point on the plane is represented as a stuct. A square is represented as a struct of four points. define corresponding types and functions for their initialization and "printing". Communication with functions by parameter only(only the main function is allowed to get data from the keyboard!) design also:
    1- A function which check wheter four points really define a square.
    2-A function which given a point and a value of the area of a square as input parameters returns four squares(define corresponding type) with sides parallel to x axis and y axis"


    i started like this:
    Code:
    #include <stdio.h>
    #include<math.h>
    struct point{
    float x;
    float y;
    }
    typedef struct point POINT;
    struct square{
    struct point p1;
    struct point p2;
    struct point p3;
    struct point p4;
    }
    typedef struct square SQUARE;
    
    int main()
    {
    int point;
    printf("point coordinate");
    printf("\n\n");
    
    printf("enter data\n");

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What don't you understand?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    what function i can use to check whether the four points in the pland define a square?
    can you explain me more the 2nd question

    thanks a looot

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The second question does sound very vague. I recommend you contact whomever wrote the assignment to clarify what he meant.

    As for your first question try to think a little bit what the attributes of a square are in a 2D coordinate field. What do the points that make the square have in common?

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    the distance between each points is eaqual, but how can i express this in programming language??

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by claudiu View Post
    The second question does sound very vague. I recommend you contact whomever wrote the assignment to clarify what he meant.
    The second question is trivial. You input a point and an area (which allows you to work out the side length). You are then required to generate four squares. Each is required to have one corner on the point you input, and their sides must be parallel to either x or y axis.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    @grumpy:can you please write me a possible code for that.. i am confused

    thank you

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by grumpy View Post
    The second question is trivial. You input a point and an area (which allows you to work out the side length). You are then required to generate four squares. Each is required to have one corner on the point you input, and their sides must be parallel to either x or y axis.
    Yeah that's what I thought of originally however there was no mention that the 4 squares shared the given vertex, which is why I was confused about it. I guess there is no other way to interpret it.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Q. How do you know if I give you two pairs of coordinates, if they form an edge of a square?

    A. Compare their Y or X values. If either Y or X are the same between a pair of coordinates, then they share an edge.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by osabri View Post
    @grumpy:can you please write me a possible code for that.. i am confused
    I can but I won't.

    The reason your teacher has given you the assignment is for you to learn. An essential part of the learning process is working out how to translate a problem description into code that addresses the problem. You will learn nothing if someone else writes the code for you.

    For the first question, try working out how you would determine if four points (a set of 4 (x,y) coordinates) were a square, if you were chained to a desk with no computer in sight and told you could not leave until you had worked out the answer. Once you have done that, work out how to translate your approach into code.

    For the second question, imagine you are chained your desk again. You have been given a point and a value for an area. Work out how you would calculate the length of side for a square with that area. Now work out how you would specify four squares that each have one corner at that point, and each has a side of the calculated length. Once you have done that, then work out how to express your approach in code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by quzah View Post
    Q. How do you know if I give you two pairs of coordinates, if they form an edge of a square?

    A. Compare their Y or X values. If either Y or X are the same between a pair of coordinates, then they share an edge.


    Quzah.
    What if the square is on an angle?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then you have to ask your teacher what they really want you to check for. What about a rhombus or a rectangle?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Then you have to ask your teacher what they really want you to check for. What about a rhombus or a rectangle?


    Quzah.
    I don't know; "square" seems pretty specific. (It's just that not all squares are axially aligned -- think of say (1,0), (0,1), (-1,0), (0,-1). That's still a square, just rotated to look like a diamond.)

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well a test for a rectangle passes as a test for a square, unless you specifically test to make sure the opposite points are across from eachother.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Well a test for a rectangle passes as a test for a square, unless you specifically test to make sure the opposite points are across from eachother.


    Quzah.
    Only if you're really bad at making a test for a square. A test for a square should check for
    1. all four sides the same length
    2. perpendicular sides
    3. no crossings, if you want to get fancy/handle weird or unsorted input

    Alternatively you could check that each diagonal is a perpendicular bisector of the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. USB1: Undefined reference to...
    By Oaktree in forum C Programming
    Replies: 6
    Last Post: 01-21-2010, 11:33 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM