CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    [RESOLVED] math help needed.. how to calculate points of a rectangle?

    Hello,

    I have a few pieces of information:

    x, y point number 1
    x, y point number 2
    angle where point 1 would intersect with point 2
    width of rectangle
    length of rectangle

    so here is a simple example for the following values:

    x1, y1 = (0,0)
    x2, y2 = (4, 0)
    angle = 90 degrees
    width = 4
    length = 5


    so my rectangle points for this would be:

    upper left: (4, 2)
    lower left: (4, -2)
    upper right: (9, 2)
    lower right: (9, -2)

    How can I write code to compute these points with the information given? Can someone provide example code?

    Thanks!

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: math help needed.. how to calculate points of a rectangle?

    Quote Originally Posted by lab1 View Post
    How can I write code to compute these points with the information given? Can someone provide example code?
    There's no point writing code if you don't have an algorithm. First find an algorithm, then convince yourself that it works and then worry about how to implement it.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    yeah I am bad at math so I was asking for help with an algorithm. can you help?

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: math help needed.. how to calculate points of a rectangle?

    Quote Originally Posted by lab1 View Post
    Hello,

    I have a few pieces of information:

    x, y point number 1
    x, y point number 2
    angle where point 1 would intersect with point 2
    width of rectangle
    length of rectangle
    What? I've never heard of a point intersecting with another.

    Quote Originally Posted by lab1 View Post
    so here is a simple example for the following values:

    x1, y1 = (0,0)
    x2, y2 = (4, 0)
    angle = 90 degrees
    width = 4
    length = 5


    so my rectangle points for this would be:

    upper left: (4, 2)
    lower left: (4, -2)
    upper right: (9, 2)
    lower right: (9, -2)

    How can I write code to compute these points with the information given? Can someone provide example code?

    Thanks!
    Could you explain please? You make it look like it is obvious, but this makes no sense to me.

    PS: As a matter of fact, successfully explaining it is 50% of the work
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    Hello,

    Sorry about my lack of explanation. What I meant is the angle of a perpendicular line at point 2, if a line connected points 1 and 2. Here is a picture below:

    Code:
                                            /
                                           /
    x1, y1=(0,0)                        c /       
             *--------------------------*/
                        x2, y2 = (4,0)  /
                                       /
                                      /
    So the points are the *'s. The angle is where the 'c' is. So in my drawing the angle is a bit greater than 90 degrees.

    In my previous example I gave the angle as 90 degrees. So, it would look like this:

    Code:
                                        |
    x1, y1=(0,0)                      c |       
             *--------------------------*
                        x2, y2 = (4,0)  |
                                        |
    See what I mean now?

    Thanks!
    Last edited by lab1; August 6th, 2010 at 08:35 AM.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: math help needed.. how to calculate points of a rectangle?

    Sounds like a trig problem to me.

    Lets' make it simpler:

    Assuming you have
    P1(0, 0)
    P2(x, 0)
    The first segment is horizontal.
    And the rectangle width and height are known (as W and H).
    You want to place your rectangle at an angle theta to the horizontal line.

    Take out a piece of paper and try to find the coordinates of the lower left angle. Draw as much as you want, and replace with numbers if you find it helps.

    Once you find the formula, the fun begins.

    PS. It's not that I don't want to give it to you, but if you can't find it on your own, you stand no chance at coding it. You need to do the effort, and then we'll help.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    The problem is it is more complex that that. The x2, y2 point could be anywhere.. to the left of, to the bottom of, etc. of course I already did the paper exercise, but the angle could also bee something different then 90 degrees. I put hours of effort into this before I posted.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: math help needed.. how to calculate points of a rectangle?

    if I understand what you mean then it should be

    "upper left": z2 - M(-t)(z2-z1) * w / ( 2|z2-z1| )
    "lower left": z2 + M(-t)(z2-z1) * w / ( 2|z2-z1| )
    "upper right": z2 - M(-t)(z2-z1) * w / ( 2|z2-z1| ) - M(-t-pi/2)(z2-z1) * h / |z2-z1|
    "lower right": z2 + M(-t)(z2-z1) * w / ( 2|z2-z1| ) - M(-t-pi/2)(z2-z1) * h / |z2-z1|

    where z1, z2 are the two points, || is the usual morm, t is the angle as you defined it and M(t) is the usual rotation matrix:

    cos(t) -sin(t)
    sin(t) cos(t)

    it should be straightforward to translate this into coordinate-wise formulas and then into code...

  9. #9
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    can anyone provide code for this? I am totally lost here.. I think code will help me better understand it

    Thanks

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: math help needed.. how to calculate points of a rectangle?

    IF you know matrixial calculation, and you understand what a rotational matrix is, than superbonzo already gave you the formula.

    If not, you'll have to expand the 2D formula to 2x 1D formulas.

    I understand your P2 could be anywhere. When tasked with a hard problem you don't know how to tackle, always start by simplifying it to something you can handle, and then add in more complexity. That said, superbonzo's formula works for everything you need.

    To do matrixes in C++, you can either write them yourself (not too recomended), use a widelly used public library (is this for school, it might not be acceptable).

    A third solution (I would recommend) would be to use complex algebra. Just use superbonzo's formula, but replace M(t) with ( cos(t) + i sin(t) ).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  11. #11
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    So I can just replace "M(-t)" with "(cos(t) + i sin(t))" ? But what is 'i' here?

    Also how do I represent somethig like: "2|z2-z1|" in code?

    Thanks!

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: math help needed.. how to calculate points of a rectangle?

    So you don't know matrix math or complex numbers?

    Well, the expression can be expanded out easily enough, that's just the compact form.

  13. #13
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    hmm complex numbers, ok.. so in C++ code I would replace M(-t) with: cos(t) + sqrt(-1)*sin(t) ?

    and to do the norm I would using the complex header file and just:

    2*norm(z2-z1) ?

    But I guess I am abit confused because the norm() doesnt take an x,y point, but rather just an integer.. right?

    Also, since I dont know how to do matrix calculations in C++, can someone suggest how to expand it to 2 1D functions?

    Thanks
    Last edited by lab1; August 6th, 2010 at 10:15 AM.

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: math help needed.. how to calculate points of a rectangle?

    No, you'd use a an object called complex:
    Code:
    std::complex myComplexNumber(cos(t), sin(t));
    or
    std::complex myComplexNumber = polar(rho, t);
    You can also use abs for calculating |z2-z1|. You could also simplify superbonzo's formula with a bit of arg:
    Code:
    std::complex upperLeft = z2 + polar( w/2, arg( z2 - z1 ) + t);
    PS: and it's c++ code too. Here, z2 and z1 are the complex coordinates of P1 and P2. t is a real number representing theta, expressed in radians.

    Since you didn't tell use how t was oriented, this formula may or may not generate the desired result.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  15. #15
    Join Date
    Apr 2001
    Posts
    1,029

    Re: math help needed.. how to calculate points of a rectangle?

    "Since you didn't tell use how t was oriented, this formula may or may not generate the desired result. "


    What exactly do you mean here? Did my pictures not explain where t is located?

    Thanks

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured