Results 1 to 5 of 5

Thread: What an angle

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    12

    Smile What an angle

    Hello everyone, I am a custom kitchen cabinet maker by trade and visual basic
    is a new found hobby of mine. I am trying to write a program to draw simple floor
    plans to help me with my kitchen layouts.
    Well I'm off to a great start!
    I can't draw a rectangle on an angle. However, I can draw a rectangle using
    using the top and left property of the shape control. But that don't tilt.
    Could someone show me the code to draw a rectangle that tilts on
    a angle. A constant of 4 could represent the wall thickness.
    I would like to be able to draw the rectangle with the line control and then
    transfer those x,y to use the line method in a picture box at any angle that I want
    using the mouse.
    I would like to use a label to show the angle in degrees ( 0 - 360 ) that the
    rectangle is being drawn.
    I know how to use the line method to draw one line ( or side ) on an angle,
    but I don’t know the formula to draw the other three sides and keep
    all the corners square.
    I would like to thank everyone who has taken the time to look at this
    this, and especially grateful to anyone who can help with the code.

    Here is what I have:

    Form name = Form1
    StartUpPosition = Manual
    ScaleMode = 1
    Width = 7980
    Height = 7560

    Picture box name = Picture1
    Appearance = 1-3D
    BackColor = White
    AutoRedraw = True
    MousePointer = 2-Cross
    Hieght = 5535
    Left = 120
    Top = 120
    Width = 7215
    ScaleMode = 3-Pixel

    Line control named = Line1 ‘ Used for rubber banding effect
    BorderColor = Red
    Visable = False
    X1 = 120
    X2 = 224
    Y1 = 312
    Y2 = 312

    '*******************************************
    Declarations

    Dim a As Long
    Dim b As Long
    Dim c As Long
    Dim d As Long

    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    a = X
    b = Y
    c = X
    d = Y

    Line1.X1 = a
    Line1.Y1 = b
    Line1.X2 = c
    Line1.Y2 = d
    Line1.Visible = True

    End Sub

    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    c = X
    d = Y
    Line1.X2 = c
    Line1.Y2 = d

    'Label1.Caption = "Some sort of code to show the angle of the rectangle" & "Degrees"

    End Sub

    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim e As Long
    Dim f As Long
    Dim g As Long
    Dim h As Long

    e = Line1.X1
    f = Line1.Y1
    g = Line1.X2
    h = Line1.Y2

    Picture1.Line (e, f)-(g, h), RGB(50, 75, 255)

    Line1.X1 = 0
    Line1.Y1 = 0
    Line1.X2 = 0
    Line1.Y2 = 0
    Line1.Visible = False
    Picture1.Visible = False
    Picture1.Visible = True

    '*************************************************
    'All this extra code will draw a square on any angle, but not the rectangle
    'that I want to use
    Dim X1 As Long
    Dim X2 As Long
    Dim Y1 As Long
    Dim Y2 As Long
    Dim SlopeT As Long 'Top part of slope fraction
    Dim SlopeB As Long 'Bottom part of slope fraction

    SlopeT = (h - f) * 1
    SlopeB = (g - e) * -1
    X1 = e + SlopeT
    Y1 = f + SlopeB
    Picture1.Line (e + SlopeT, f + SlopeB)-(e, f), RGB(50, 75, 255)

    'the other side
    SlopeT = (f - h) * -1
    SlopeB = (e - g) * 1
    X2 = g + SlopeT
    Y2 = h + SlopeB
    Picture1.Line (g + SlopeT, h + SlopeB)-(g, h), RGB(50, 75, 255)

    'And the last line
    Picture1.Line (X1, Y1)-(X2, Y2), RGB(50, 75, 255)
    End Sub

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    more data

    to draw an angled rectangle you need position, size (as vector) and angle or position length vector and width or size, i'd prefer the second since you can use the crossproduct to get the vector to the third corner instead of trigs. How and what variables are you going to use?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Fun or for work?

    If you just want to play and learn more about VB, you have a fun project which should keep you busy.

    If you need something like this for your work, there are a lot of slick applications available cheap.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    12

    Smile What angle

    Thank you guv and kedman for your reply
    Several years ago, I built a remote control airplane. Took maybe over a year to complete in my spair time.
    I think it took 100 plus hours to build. The plane was nice, I loved it. I said, one of these days I'm going to fly this thing. But
    I was frightened of crashing and destroying it.
    So a couple of years after it was compleated. I went to a nearby field and I flew the plane. Straight up and straight down!
    LOL!
    Splinters! About $600.00
    I have a new one that is about four years old now........ I'm going to fly the thing one of these days.

    I went fishing once too!

    I guess what I'm saying is a mans got to waste time on something.

    By the way I have 3DHome Architect 3.0, which i really like and Total 3D Home. I also have a nother
    old one that I can't rember the name of. Most of the professional software is not so inexpensive, they run
    Thousands of dollors. But saying that, programming for me is to much work to be for work so I
    will call it playing, or as my wife says wasting time. I'll work tomarrow.

    Thanks again, and still I would appreaciate any help that you may offer with the code.

  5. #5
    Member
    Join Date
    Sep 1999
    Location
    Zurich, Switzerland
    Posts
    55
    To use the line control for rubber band effect is a good idea. Just adjust the coordinates and the screen is automatically redrawn. No need for complicated garphic commands.

    I made a similar thing: Move an arrow by "drag'n'drop" the arrow tip round a circle. Turning point is the circle center. The arrow was made from a few line controls, which are not capable of drag'n'drop. So I placed an empty, flat little label box below the arrow tip. When one drag'n'drops the arrow tip, he actually drag'n'drops the little flat empty label, and when the lblCursor_DragDrop event is risen the program adjusts the line control coordinates so that it matches the center of the label again.

    Saying that it is clear that you need to specify the turning point of your rectangle.

    Say, the rectangle is made from an array of line controls, line(0) to line(3). Say, one end of your line(0) control is the turning point while you drag'n'drop the other end. When the lblCursor_DragOver event is risen it is time to refresh the rectangle:

    Code:
    Const PI = 3.141592654 'Global Constant!
    
    Private Sub lblCursor_DragOver
    
    Dim CenterX as single
    Dim CenterY as single
    Dim AngleOfLine as single
    Dim AngleOfCursor as single
    Dim dx as single
    Dim dy as single
    Dim AngleDiff as single
    Dim i as integer
    
    'Set the Center point aka turning point
    CenterX = line(0).x1
    CenterY = line(0).y1
    
    'Calculate the old angle of the line
    'Watch that on the screen, y coordinates are all negative
    dx = line(0).x2 - CenterX
    dy = -line(0).y2 + CenterY
    AngleOfLine = Atn(dy / dx) / PI * 180 'Angle results in degrees
    
    'Calculate the angle of the cursor
    dx = lblCursor.left + lblCursor.Width / 2 - CenterX
    dy = -(lblCursor.Top + lblCursor.Height / 2) + CenterY
    AngleOfCursor = Atn(dy / dx) / PI * 180 'Angle results in degrees
    
    'Calculate the Angle Difference
    AngleDiff = AngleOfCursor - AngleOfLine
    
    'Now turn your rectangle
    For i = 0 to 3
        Turn line(i), CenterX, CenterY, AngleDiff
    Next i
    
    End Sub
    
    Private Sub Turn(MyLine as control, CenterX as single, CenterY as single, AngleDiff as single)
    'Turns the given line by AngleDiff round the center
    
    Dim Angle as single
    Dim Radius as single
    
    'Calculate the Length of the Line aka Radius
    Radius = sqr((Myline.x2 - Myline.x1)^2 + (-Myline.y2 + Myline.y1)^2) 'Pythagoras
    
    'Calculate the old angle of the start point
    AngleOfLine = Atn(-Myline.y1 + CenterY / Myline.x1 - CenterX) / PI * 180 'Angle results in degrees
    
    'Shift Angle
    AngleOfLine = AngleOfLine + AngleDiff
    
    'Calculate new coordinates of the start point
    Myline.x1 = CenterX + Radius * cos(AngleOfLine * PI / 180)
    Myline.y1 = CenterY - Radius * sin(AngleOfLine * PI / 180)
    
    'Calculate the old angle of the end point
    AngleOfLine = Atn(-Myline.y2 + CenterY / Myline.x2 - CenterX) / PI * 180 'Angle results in degrees
    
    'Shift Angle
    AngleOfLine = AngleOfLine + AngleDiff
    
    'Calculate new coordinates of the end point
    Myline.x2 = CenterX + Radius * cos(AngleOfLine * PI / 180)
    Myline.y2 = CenterY - Radius * sin(AngleOfLine * PI / 180)
    
    End Sub
    There is one thing left: If one releases the mouse, the label must be reset to the end of line(0):
    Code:
    Private Sub lblCursor_DragDrop
    
    'Reset the label to the end of line(0)
    lblCursor.left = line(0).x2 - lblCursor.Width / 2
    lblCursor.Top = line(0).y2 - lblCursor.Height / 2
    
    End Sub
    To be honest I havent checked the code but I see no major problem with it. Just give it a try, if you want, and if you have problems with it, post again. Hope this helps a bit. Have fun!

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