Results 1 to 23 of 23

Thread: [RESOLVED] fit outsized polygon inside picturebox and rotate by 180°

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Resolved [RESOLVED] fit outsized polygon inside picturebox and rotate by 180°

    Hi All
    I am trying to fit an outsized shape into a picture box, and then invert it by 180 degrees.
    I managed to make it move with aid of trackbar up / down but I am unable to rotate it
    using 'RotateFlip' commands, so I try to find a factor that I could use to make it work.
    Could someone help me please ?
    thanks in advance

    Name:  invertVB.jpg
Views: 3938
Size:  21.0 KB


    Code:
    '
    ' 26 Jan 2014       ' fit outsized polygon inside picturebox and rotate by 180°
    '
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Public Class Form1
        Public limArr(5, 2) As String
        Public maxObj As Integer
        Public higherObjX, higherObjY As Double
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            End
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' draw
            Call DrawResizeInvert()
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            ' there is a form with a picturebox of defined dimensions and known location on screen.
            ' there is also an object(polygon) of defined dimensions that needs to be
            ' displayed FLIPPED on the picturebox and RESIZED to fit in the picturebox.
            '
            ' GATHER INITIAL PARAMETERS
            ' picturebox related data
            Dim picX, picY, picSizeW, picSizeH As Integer
            picX = PictureBox1.Location.X
            picY = PictureBox1.Location.Y
            picSizeW = PictureBox1.Size.Width
            picSizeH = PictureBox1.Size.Height
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            '-----------------------------------------------------------------
            ' object - POLYGON related data
            '
            Dim n As Integer
            n = 0
            higherObjX = 0
            higherObjY = 0
            limArr(1, 1) = "2" : limArr(1, 2) = "40"
            limArr(2, 1) = "500" : limArr(2, 2) = "10"  ' 2,1 = outside pic box height
            limArr(3, 1) = "500" : limArr(3, 2) = "180" ' 3,1 = outside pic box height
            limArr(4, 1) = "2" : limArr(4, 2) = "120"
            '------------------------------------------------------------------
            ' avoid exceeding the array limits (bounds)
            On Error GoTo 10
            ' find total array items
            maxObj = 0
            For n = 1 To 10
                If limArr(n, 1) = "" Then Exit For
                maxObj = maxObj + 1
                If Val(limArr(n, 1)) > Val(limArr(n - 1, 1)) Then higherObjX = Val(limArr(n, 1))
                If Val(limArr(n, 2)) > Val(limArr(n - 1, 2)) Then higherObjY = Val(limArr(n, 2))
            Next
            '----------- initial parameters gathered ------------------
            TrackBar1.Value = 0
            Label1.Text = TrackBar1.Value
            TrackBar1.Enabled = False
            '----------------------------------------------------------
            Exit Sub
    10:
            MsgBox("Some error occured with the array index...")
        End Sub
        Sub DrawResizeInvert()
            On Error GoTo 10
            '---------------------------
            TrackBar1.Enabled = True
            TrackBar1.BackColor = Color.LightSteelBlue
            PictureBox1.Refresh()
            '---------------------------
            Dim G As Graphics
            G = PictureBox1.CreateGraphics
            G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            Dim P As New Pen(Color.Blue)
            Dim picAR   ' original picture box aspect ratio
            picAR = (PictureBox1.Size.Height / PictureBox1.Size.Width)
            '--------------------------------------------------------------
            ' image is out of screen so we must SCALE it down...
            ' reduce shape on one or more axis(es) until it fits the picture box
            ' so find the SF (scaling factor) that will reduce one side to fit inside the picture box.
            Dim SF, n, curHighX
            n = 0
            Do
                n = n + 0.05
                SF = n * picAR                  ' get the Scale Factor that will bring the object inside the picture box
                curHighX = (higherObjX / SF)
                If curHighX < (PictureBox1.Size.Height - 1) Then Exit Do
            Loop
            '--------------------------------------------------------------
            ' to maintain proportions must scale all items / both axes 
            Dim X(10), Y(10) As Integer
            For n = 1 To maxObj
                If Val(limArr(n, 1)) < higherObjY Then
                    Y(n) = Int(Val(limArr(n, 1)) / 1)      ' no need to scale
                ElseIf Val(limArr(n, 1)) >= higherObjY Then
                    Y(n) = Int(Val(limArr(n, 1)) / SF)     ' scale per SF value
                End If
                If Val(limArr(n, 2)) < higherObjX Then
                    X(n) = Int(Val(limArr(n, 2)) / 1)
                ElseIf Val(limArr(n, 2)) >= higherObjY Then
                    X(n) = Int(Val(limArr(n, 2)) / SF)
                End If
            Next
            '/////////////////////////////////////////////////////////
            ' the PROBLEM AREA
            ' need to display the shape inverting with the aid of trackbar
            ' which will display the actual INVERT FACTOR to achieve 180°
            ' creating a 'mirror' of the origial shape (polygon)
            '
            ' horizontal / vertical rotation ?
            '/////////////////////////////////////////////////////////
            For n = 1 To maxObj
                If Y(n) < higherObjY Then
                    Y(n) = Y(n) - TrackBar1.Value
                ElseIf Y(n) >= higherObjY Then
                    Y(n) = Y(n) - TrackBar1.Value
                End If
            Next
            '-------------------------------------------------------------
            ' draw and show !
            For n = 1 To maxObj + 1
                If n < maxObj Then G.DrawLine(P, X(n), Y(n), X(n + 1), Y(n + 1))
                If n = maxObj + 1 Then G.DrawLine(P, X(n - 1), Y(n - 1), X(1), Y(1)) ' close polygon with the first (1) point !
            Next n
            Exit Sub
    10:     MsgBox("Some error occured with inverting the image...")
        End Sub
        Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
            Label1.Text = TrackBar1.Value
            Call DrawResizeInvert()
        End Sub
    End Class

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Rather than scale and draw the polygon manually, I would create a graphicsPath and a transformation Matrix. You would be adding the lines for the polygon to the graphics path and then using the matrix to scale, translate and rotate the path. It will take some "figuring out" on you part, but it will cut out a lot of the messy mathy details and ultimate make your code more manageable.

    Now onto something more pressing about your code....
    Code:
    On Error GoTo 10
    Using On Error Goto is not good, but when line#10 resides in a another sub routine altogether, it just gets worse.

    The On Error should be replaced with a try catch block.
    vb Code:
    1. try
    2.     'all of the routine's code....
    3. catch ex as exception
    4.      MsgBox("Some error occurred with the array index...")
    5. End try

    Also, you are creating a graphics object for the drawing. All of the drawing on the picturebox should be done in the picturebox.Paint event handler using the provided graphics object (e.Graphic). Drawing the way you are requires your code to manual refresh. Putting it in the pB.Paint routine will ensure the picture box will be redrawn as needed.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    hi Kebo and thanks a lot.
    will try to do the 'figuring out' you are suggesting.
    now regarding the 'On Error' have an additional question.
    in a different app :

    form 1
    on error
    call to form 2
    ermsg: 'error in form 1'
    end sub
    form 2
    on error
    ermsg: ' error in form 2
    end sub

    so although the error occurs in form2, the message i get is that for the form1 and very disturbing.
    so is this (sort of 'nestsed' 'on error', what is causing it ?

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: fit outsized polygon inside picturebox and rotate by 180°

    This is a different topic than in the OP. Would you please start a separate thread for this? If you do, please include details.
    thanks
    Last edited by kebo; Jan 26th, 2014 at 12:20 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    ok Kevin, thanks again

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Quote Originally Posted by kebo View Post
    Rather than scale and draw the polygon manually, I would create a graphicsPath and a transformation Matrix. You would be adding the lines for the polygon to the graphics path and then using the matrix to scale, translate and rotate the path. It will take some "figuring out" on you part, but it will cut out a lot of the messy mathy details and ultimate make your code more manageable.
    ...
    It seems to me the "requirements" are fairly archane and yet specific enough that it sounds like an assignment where figuring out the math necessary to calculate the points is part of the exercise. Once you can work out how to manually scale and transform points it might make it easier to understand what using the matrix and transforms will be doing for you in a later lesson.

    Plus, having a date and title at the top of the code would be fairly uncommon for a newbie hobbiest, in my opinion.
    '
    ' 26 Jan 2014 ' fit outsized polygon inside picturebox and rotate by 180°
    '
    Last edited by passel; Jan 27th, 2014 at 02:20 PM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Name:  CG envelope 747.jpg
Views: 2424
Size:  65.2 KB

    dear passel hi

    it is a personal assignment. see, didn't finish high school and i have problems with math. but despite that and mainly through many many different trials and experiments, i managed, somehow, sometimes to put it all together - and also with help from good forum people - to get the result i needed. it is of course very time comsuming and i dont expect to raise myself above the 'newbie hobbiest' and i dont need it too.
    so sometimes i try o accomplish things without having solid foundation but through prayer and persistence. the particular polygon is a typical jetliner 'weight and balance envelope'. but it is inverted. weight start at bottom 10k,30k 100k moving up, whereas the picturebox starts at 0k, moving down 10 20 etc. that is why i need to invert the shape, not the picturebox, so i m struggling with the math.
    change nbr - compile - run - gee does not work ! so again try another nbr - recompile etc, thats why something that appears so simple (in my view at least) takes forever to create properly.
    maybe to big for my shoes but i believe everybody can do a little basic programming with the pc, regardless education. it is a nice hobby.
    as for the date, i seen it at other peoples code, but also helps me to compare it to my other trials.

    God bless
    Last edited by vangos; Jan 28th, 2014 at 03:16 AM. Reason: add image

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: fit outsized polygon inside picturebox and rotate by 180°

    In that case, I guess I agree with kebo.
    Perhaps you're not there yet, but VB.Net because of GDI+, does support using matrix transforms so you make the scaling anything you want, at least linearly, and change it on the fly, so you can have 0,0 at whatever point on the drawing surface is suitable for your drawing needs, and move it around as you do different parts of the drawing if needed. You can scale X and Y by different amounts and directions, to match the X and Y scale of your graph, and have Y going up, not down, etc.
    I don't have a good tutorial that would help with doing that type of chart drawing. I have a lot of little things. Once thing that I could post, is a test application that was the start of drawing an ADI type instrument, and another page that were other steam type gages, but never finished as I didn't need it for anything. Did in in VB6 originally, then a person who primarily worked with C# asked if he could do that type of graphics in C#, and I said sure, so worked out an example, setup to have a built in "tutorial" type explanation, step by step, of what drawing methods were used to do the various parts of the drawing.
    After doing the C# version, which is not something I usually write in but since the methods called are pretty much the same as you would do from VB.Net, wasn't hard to do, I went ahead and created a VB.Net version.
    Rather than reiterate all the details here, you can check out the post here.

    p.s. I don't consider myself much of a math person myself. Did finish High School, but it was one of the worse schools in the state academically, went into the Navy to learn how computers work and work with/on them and supporting systems for six years, so know my binary math and how computers work (or at least how they worked 20 years ago), pretty well. VB has mostly been a hobby for me as well, while programming in other languages has been the majority of my job for the last 30 years. Lately, I have had the opportunity to write support tools and applications for my work using whatever I want, so initially did a few in VB6, because I was quick at it, and have moved to VB.Net for the last 3 years where I can. Otherwise, the rest of my work is C, with a little C++.
    Last edited by passel; Jan 28th, 2014 at 12:16 PM.

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Hi vangos,
    There's several easy ways to turn your graphics the right way up. Since you are just connecting up points with DrawLine, you could replace each of the Y values by PictureBox.Height - Y. But you have a lot of points and lines to change, so it would be less code to do it using a graphics transform:

    Code:
    Dim w As Integer= PictureBox.ClientSize.Width\2
    Dim h As Integer = PictureBox.ClientSize.Height\2
    g.TranslateTransform(w, h)
    g.RotateTransform(180)
    g.TranslateTransform(-w, -h)
    Put that code before the DrawLines instructions (or any other graphics instructions that have to be flipped).

    If you want to go on to use more graphics methods without flipping them, put
    Code:
    g.ResetTransform
    before the new instructions.

    BB

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Dears passel and BB

    thank you very much for all your king help.
    I used successfully the 'g.RotateTransform(trackbar1.value)' iso 180 and it does it !!! Wow, didn't expect it needed only few lines...

    One last thing I would like to ask, if I may. As I move the trackbar it moves anti- or clockwise. How can I make it flip or invert (not sure which is the correct term ) on its lateral axis alone ?
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Sorry guys, picture didn't go where supposed to...it was meant for the profile...

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: fit outsized polygon inside picturebox and rotate by 180°

    I guess I wouldn't use the trackbar value direct.
    Decide at what value you want it to flip, and change the angle to either 0 or 180.
    (being as you only want it to flip, perhaps a trackbar isn't what you want anyway, it seems like two buttons would do).

  13. #13
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: fit outsized polygon inside picturebox and rotate by 180°

    I'm not quite sure if it's what you mean, but I guess you want to flip the image without rotating it; in other words, to make an upside-down mirror image. You can't do that just by rotating, but this method will work:

    Code:
    Using mtx As New Drawing2D.Matrix(1, 0, 0, -1, 0, 0)
    	mtx.Translate(0, -PictureBox1.ClientSize.Height)
    	g.Transform = mtx
    End Using
    That will flip the image around the x-axis (i.e. turn it upside down).

    If you want to flip it around the y-axis (mirror left to right) change it to this:
    Code:
    Using mtx As New Drawing2D.Matrix(-1, 0, 0, 1, 0, 0)
    	mtx.Translate(-PictureBox1.ClientSize.Width, 0)
    	g.Transform = mtx
    End Using
    By the way, you had me worried for a moment. If you want to transform your weight and balance envelope into that rugged portrait, it will take a lot more than a few graphics transforms.

    BB

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Name:  polyg2.jpg
Views: 2265
Size:  24.5 KB

    That's great guys !!!! Thanks a lot, this made it work :

    Using mtx As New Drawing2D.Matrix(1, 0, 0, -1, 0, 0)
    mtx.Translate(0, -PictureBox1.ClientSize.Height)
    g.Transform = mtx
    End Using
    I was hoping to use the trackbar, to watch the envelope inverting as shown by the red lines in the jpg,
    but i guess I can hook it up to one of the matrix parameters and watch it happening, right ?

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    so.. I managed to get it to the center with the sliders and the 'center button' but it stays small ..


    Name:  polyg3.jpg
Views: 2988
Size:  33.8 KB

  16. #16
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Your probably realize that everything you can do with the Graphics methods -- RotateTransform, TranslateTransform and ScaleTransform -- you can also do with a matrix. I prefer using the Graphics methods to using a matrix, among other reasons because it saves explanation when you want to communicate it to other people.

    Mirror-image flipping is the only exception. It is effectively the same as scaling by -1 in the X or Y direction. You need a matrix to do that because ScaleTransform(1, -1) isn't allowed: the ScaleTransform statement won't accept negative arguments.

    By default, all the Transformations act at the origin, which in this case is the top left corner of the picture box. As Passel explained in post #8, you need to shift the origin temporarily to rotate or scale around some other point such as the centre of the picture box. That is why the TranslateTransform statements were needed in the code in post #9. You will need similar Translate statements if you want to scale the image relative to a point in the picture box.

    BB

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    oh, so that's why it keeps rotating in left top / left bottom (after applying matrix).... i was hoping for a display similar to 'flipping a coin effect' but maybe its a lot of hard work ahead to get to that...
    I am not sure but think in Turbo Basic ca. 1990-1992 there was a thing like 'World Coords' or something that cant find here...


    Name:  poly flip 1.jpg
Views: 2170
Size:  42.2 KB
    Last edited by vangos; Jan 29th, 2014 at 04:15 PM.

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: fit outsized polygon inside picturebox and rotate by 180°

    I figured that was what you wanted after post#14. I hadn't tried it yet so didn't remember that you couldn't use negative scaling in the Graphics methods, which is also what I usually use. About the only time I use a matrix object is when there is a need to persist a matrix for use in other places, such as converting mouse coordinates into World coordinates.
    boops boops has already shown if you want to use negative scaling then you have to use a matrix object.
    So, I would make that matix object persistent (either static in the procedure, or form scope) and then you could modifiy the Y scale factor between the values of -1 and 1 (e.g. 1.0 0.9, 0.8. 0.7 etc) as you move the trackbar to "animate" the flip by compressing/reversing/uncompressing the Y axis.
    I guess it may not take a long time to create a new matrix each time, compared to how long it takes draw, but I'm of the inclination of "if I'm going to reuse an object possibly a hundred times a second, then don't waste time destroying and recreating it repeatedly".

    p.s. I tested it and ScaleTransform did allow negative arguments, so I didn't use a separate matrix in this example.
    The one precaution needed is scaling by 0 is not valid, so had to make sure 0 was not passed to ScaleTransform.
    Of course, it doesn't look too "coin" flippish to me because we don't have a perspective view. It just looks like it shrinks, and then expands in a reverse direction. Perhaps a more irregular shape might look more flippy, but I doubt it, without some perspective foreshortening of the drawing, which would be more complicated.
    Anyway, add a vertical trackbar toward the right side of a form, and give the code a try. Everything is hardcoded, but if the client area of the form is at least 300 pixels high, it should look fine.

    p.p.s I put a gap between the two rectangles, and now it looks a bit more flippish to me, as the gap narrows so you get some sense of movement. Originally the values were (,-100,-100,100,100) and (,-100,0,100,100) which had both rectangles meet on the X axis (where Y = 0, aka the center of our flip "rotation").
    Code:
    Public Class Form1
    
      Dim yScaleValue As Single = 1
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DoubleBuffered = True
        TrackBar1.Minimum = -100
        TrackBar1.Maximum = 100
        TrackBar1.Value = 100  'start fully "Up"
      End Sub
    
      Private Sub TrackBar1_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar1.Scroll
        yScaleValue = TrackBar1.Value / 100
        Invalidate()
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        Dim localScale As Single = yScaleValue
        If Math.Abs(localScale) < 0.01 Then  '0 will break our drawing so make sure we don't scale by 0
          localScale = 0.01
        End If
    
        g.TranslateTransform(200, 200)
        g.ScaleTransform(1, localScale)
        g.DrawRectangle(Pens.Red, -100, -110, 100, 100)
        g.DrawRectangle(Pens.Blue, -100, 10, 100, 100)
      End Sub
    End Class
    Last edited by passel; Jan 29th, 2014 at 06:30 PM.

  19. #19
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Quote Originally Posted by passel View Post
    p.s. I tested it and ScaleTransform did allow negative arguments, so I didn't use a separate matrix in this example.
    The one precaution needed is scaling by 0 is not valid, so had to make sure 0 was not passed to ScaleTransform.
    Thanks for correcting me on that. I noted that "error" years ago but clearly misinterpreted it.

    @vangos: I hope you are now following Passel's example of the right way to do drawing in GDI+: using the Graphics object in a Paint event handler. Since you are using a PictureBox, it's probably clear to you that you need to code the Paint event of the PictureBox rather of the Form. Also, in the trackbar event handler, use PictureBox1.Invalidate to trigger painting of the picture box when the trackbar moves.

    BB

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Dear passel and BB

    Thanks again, yes I ll try to use passel's code to the extend possible, Thank you both.

    @passel what is amazing is that years ago i was trying to write code for an artificial horizon (what now called ADI or PFD ) ( i 've even got my hands into an old Phantom horiz ) so I think I will also revisit that project and will let you know. But what you made is excellent !!

    Name:  ahoriz.jpg
Views: 2000
Size:  44.1 KB
    Last edited by vangos; Jan 30th, 2014 at 05:39 AM.

  21. #21
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: fit outsized polygon inside picturebox and rotate by 180°

    I have an old ADI similar to that myself, which was modified for simulator use 30 or so years ago, but wasn't functional when it came into my possession. It also had director bars and other flags, but unfortunately with all the travels, some have broken off and lie at the bottom of the glass.

    It was given to me for a reference as I was to recreate one on a display (this was 20 years ago) driven by a Silicon Graphics machine, using GL (from which OpenGL was derived). But the project was cancelled before I got to the task of doing the display.

    The ADI I did in my example, was partly based on some on-line FAA pdf document describing several Glass Cockpit displays (I wouldn’t have extended the horizon outside the instrument otherwise) and other glass cockpit display that I had seen.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    Re: fit outsized polygon inside picturebox and rotate by 180°

    Well guys thanks. Success thanks to you !!

    Name:  flipped halfway at last.jpg
Views: 3022
Size:  53.0 KB

    and the code...

    Code:
    ' 31-JAN-2014
    ' code by passel and BB from vbForums
    '
    Public Class passel
    
        Dim yScaleValue As Single = 1
        Public z, w
    
        Private Sub passel_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            TrackBar1.Minimum = -100
            TrackBar1.Maximum = 100
            TrackBar1.Value = 50  'start half-way up            'fully "Up"
            yScaleValue = 0.5
        End Sub
    
        Private Sub passel_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    
            Dim g As Graphics = e.Graphics
            Label1.Text = TrackBar1.Value   'yScaleValue
            'g = PictureBox1.CreateGraphics
            Dim localScale As Single = yScaleValue
            If Math.Abs(localScale) < 0.00001 Then  '0 will break our drawing so make sure we don't scale by 0
                localScale = 0.01
            End If
            '------------------------------------------------------------------------------------------------------
            Label1.Text = yScaleValue
            z = 0                       ' counter for later use
            '------------------------------------------------------------------------------------------------------
            Dim limArr(20, 2)
    
            limArr(1, 1) = "2" : limArr(1, 2) = "38.4"        ' BOEING 747 WEIGHT & BALANCE ENVELOPE DATA
            limArr(2, 1) = "155.825" : limArr(2, 2) = "30.4"
            limArr(3, 1) = "172.155" : limArr(3, 2) = "15.8"
            limArr(4, 1) = "235.658" : limArr(4, 2) = "7.8"     ' 335
            limArr(5, 1) = "257.842" : limArr(5, 2) = "8.7"     ' 5,1 = outside pic box height 377
            limArr(6, 1) = "257.842" : limArr(6, 2) = "59.4"    ' 377
            limArr(7, 1) = "252.894" : limArr(7, 2) = "78.1"    ' 352
            limArr(8, 1) = "235.658" : limArr(8, 2) = "97.6"    ' 335             8,2 = outside pic box width
            limArr(9, 1) = "2" : limArr(9, 2) = "71.6"
            limArr(10, 1) = "2" : limArr(10, 2) = "38.4"
    
            Dim X(10), Y(10)
    
            For n = 1 To 10
                ' these coords show envelope 'plagiasmeno' resting on one of long sides
                'X(n) = Int(Val(limArr(n, 1)))
                'Y(n) = Int(Val(limArr(n, 2)))
                '
                Y(n) = Int(Val(limArr(n, 1)))
                X(n) = Int(Val(limArr(n, 2)))
                '
                'Dim ptsTow() As Point = {New Point(X(n), Y(n))}        ' does not work, must define one-by-one ?!
            Next
    
            Dim ptsTOW() As Point = { _
                   New Point(X(1), Y(1)), _
                   New Point(X(2), Y(2)), _
                   New Point(X(3), Y(3)), _
                   New Point(X(4), Y(4)), _
                   New Point(X(5), Y(5)), _
                   New Point(X(6), Y(6)), _
                   New Point(X(7), Y(7)), _
                   New Point(X(8), Y(8)), _
                   New Point(X(9), Y(9)), _
                   New Point(X(10), Y(10))}
    
            Dim pp As New Pen(Color.Magenta, 1.8)
    
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
    
            g.DrawLine(pp, 10, 300, 300, 300)   ' draw X axis - mid screen (axis of rotation)
    
            g.TranslateTransform(300, 300)
            g.ScaleTransform(1, localScale)
            '                                                               g.DrawRectangle(Pens.Red, -100, -110, 100, 100)
            '                                                               g.DrawRectangle(Pens.Blue, -100, 10, 100, 100)
    
            g.DrawPolygon(pp, ptsTOW)
    
        End Sub
    
        Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
            yScaleValue = TrackBar1.Value / 100
            'If yScaleValue = 0 Then yScaleValue = 0.01
            If yScaleValue = -1 Then
                '    yScaleValue = 1
                'TrackBar1.Value = 1
            End If
            Invalidate()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'DRAW - start timer
            'yScaleValue = TrackBar1.Value / 100
            'If yScaleValue = 0 Then yScaleValue = 0.01
            'Invalidate()
            Timer1.Enabled = True
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Me.Close()
            Form1.Show()
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            TrackBar1.Value = 0.01
            Label1.Text = TrackBar1.Value
            Timer1.Enabled = False
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            ' stop timer
            Timer1.Enabled = False
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            'If z = 2 Then Timer1.Stop() : Timer1.Enabled = False : MsgBox("DONE")
            Call doIt() ' every second or milisec, cant correlate ticks and yScalevalues...
        End Sub
    
        Sub doIt()
    
            Try
    
                LabelT.Text = Mid(Now, 12, 9)                ' update form clock
    
                If z = 0 Then
                    Label5.Text = "A"
                    yScaleValue = yScaleValue - 0.01
                    If yScaleValue <= 0 Then z = 1
                End If
    
                If z = 1 Then
                    Label5.Text = "B"
                    yScaleValue = yScaleValue - 0.01
                    If yScaleValue <= -0.5 Then yScaleValue = -0.5 : z = 2 ' -.55 or -0.60
                End If
    
                If z = 2 Then
                    Label5.Text = "C"
                    yScaleValue = yScaleValue + 0.01    ' decrease -0.43, -0.40, .. at zero jump to +0.01 and to D
                    If yScaleValue > 0.01 Then yScaleValue = 0.5 : z = 3
                End If
    
                If z = 3 Then
                    Label5.Text = "D"
                    yScaleValue = yScaleValue + 0.01            ' Val(LabelT.Text) + 0.01
                    If yScaleValue > 0.5 Then z = 0 : yScaleValue = 0.5
                End If
    
                Invalidate()                                 ' refresh form / show current drawing
    
                Label1.Text = Format(Int(yScaleValue), "#,##0.00")
    
            Catch ex As Exception
                MsgBox("something wrong with the Timer !")
            End Try
        End Sub
    
    End Class

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Location
    Athens, Greece
    Posts
    30

    RESOLVED : fit outsized polygon inside picturebox and rotate by 180°

    Resolved

Tags for this Thread

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