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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  6. #6
    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.

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

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

    thank you

  9. #9
    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.

  10. #10
    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.

  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
    Can you plot a nonsquare that breaks this test?

    Make one pair of X axis values.
    Make one pair of Y axis values.

    Between the remaining four numbers, there must be two pairs. Then it's a square. I was originally going to say you'd have two pairs of numbers for each axis, but angled squares throw that off. So I think the above works.

    "What?"

    Consider:

    0,0 | 0,1
    1,0 | 1,1

    We have a pair of matching Y axis (1s), and a pair of matching Xs (0s). We also have two more pairs between them: 1s and 0s. It's a square. Now we take your example:

    (1,0), (0,1), (-1,0), (0,-1)

    We have a pair of rows: 0
    We have a pair of colums: 0
    We have two other pairs also: 1s and -1s.

    It passes this square test. I don't know if that works for everything, but if it does, it's simple.


    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
    Can you plot a nonsquare that breaks this test?

    Make one pair of X axis values.
    Make one pair of Y axis values.

    Between the remaining four numbers, there must be two pairs. Then it's a square. I was originally going to say you'd have two pairs of numbers for each axis, but angled squares throw that off. So I think the above works.

    "What?"

    Consider:

    0,0 | 0,1
    1,0 | 1,1

    We have a pair of matching Y axis (1s), and a pair of matching Xs (0s). We also have two more pairs between them: 1s and 0s. It's a square. Now we take your example:

    (1,0), (0,1), (-1,0), (0,-1)

    We have a pair of rows: 0
    We have a pair of colums: 0
    We have two other pairs also: 1s and -1s.

    It passes this square test. I don't know if that works for everything, but if it does, it's simple.


    Quzah.
    So what about the square (0,0), (3, 1), (2, 4), (-1,3)? Or the nonsquare (1,0), (0,1), (-2,0), (0,-2) that would appear to pass your test (if I've read it correctly)?
    Last edited by tabstop; 04-24-2010 at 09:54 AM.

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