Re: Nearest point to the mouse cursor?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Larry Serflaten (serflaten_at_usinternet.com)
Date: 10/04/04


Date: Mon, 4 Oct 2004 13:15:04 -0500


"Niels Jensen" <NielsJensen@discussions.microsoft.com> wrote
> Thanks for this code Larry it's *yet* another was that I can draw a hexmap...
>
> Is there any chance you could be so kind and explain what the complicated
> mathematical sections do (and how they do it?)

It is very easy to get X and Y coordinates when the cells are rectangles:

> > ' Get red grid cell x and y
> > x = e.X \ SX
> > y = e.Y \ SY

SX and SY are the size values of the red cells, so that, dividing the
current X or Y mouse position by the size of the cell give the cell's
X or Y values.

It is not so easy when there are angled lines or other such polygons.
In your case, I first set out to identify where the angled lines are, you
can see by the displayed grid they do follow a repeating pattern and
that pattern is represented in the Rows array. Second, the angle the
slopes are at is constant so one formula (based on the ratio of X to Y)
can be used to tell when the mouse crosses the angled line. When it
does, the horizontal position has to be adjusted. There is one formula
for the forward slash, and another for the backward slash. With the
correct X position, there is one other thing to address and that is that
alternating hex columns are offset by one cell difference and that is
found and altered as the last part of the process.

> I've been trying to figure
> out how the 1st section works:
>
> > Rows(0) = New Integer() {-1, 0, 0, -2, 1, 1, -1}
> > Rows(1) = New Integer() {-2, 0, 0, -1, 1, 1, -2}

It is an array of arrays.

These statements assign arrays of Integers to the elements of the Rows
array. "New Integer()" tells VB you're creating a new array and the values
inside the brackets initialize that array with those values. In this case, a -1
represents the forward slash (angle cell) and -2 represents the backward
slash (angle cell). 0 and 1 were only used during the draw process to help
determine if the horizontal line of the hex cell should be at the top or
bottom of that group of cells.

> I'm also drawing the hexcells in a panel, not a form and although I've
> successfully managed to do this within my program using your code, the
> graphics are only drawn within the initial visible part of the panel, as soon
> as I use the scrollbars I get one of two things hapening. If the drawimage
> is in the "paint" sub then it re-draws all over the screen eventually drawing
> a nice big red block on the panel screen. how can I make your hexmap draw a
> 50hex by 50hex board and then maintain the image so that the scrollbars on
> the panel display the image correctly. I'll be overlaying the Hexes that
> have information with small .bmp graphical images such as trees or towns, the
> idea being that the end user can click on a hex containing a town image and
> get the information for the town.

You will probably want to back up that panel with a memory bitmap such that
you draw your 50X50 board to the bitmap, and then copy it over to the panel
(in its Paint event) depending on the scroll position.

While doing your drawing in the paint event is a low impact method, it gets a
bit useless when you have several items to draw and are erasing the screen to
reposition them all. In that case it is better to draw to memory and then copy
the finished image in one go.

Take a look at what I did, I gave the form a public Image property that was
my memory bitmap. It is such a common methodology that I did it by habit!
In the form's Paint event I simply copied over the image in one go. Doing it
that way (and even that can be improved) allows for automatic repainting when
the form is restored from being minimized, or when some other window covers
and then exposes that form.

Where I have 0, 0 you'd add in values from your scroll control....

HTH
LFS



Relevant Pages

  • Re: Nearest point to the mouse cursor?
    ... > It is very easy to get X and Y coordinates when the cells are rectangles: ... > that pattern is represented in the Rows array. ... >> I'm also drawing the hexcells in a panel, not a form and although I've ... > you draw your 50X50 board to the bitmap, and then copy it over to the panel ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Speed Up document.getElementById()?
    ... Do this for each cell reference, whether it be inside a for loop ... lose time when building this array. ... So, until I draw the whole table, I cannot create a reference array. ...
    (comp.lang.javascript)
  • how to shrink a numarray array?
    ... To draw numarray.arangeon a panel of width of 100, ... So I need a method to shrink it to an 100-item array. ...
    (comp.lang.python)
  • RE: User List Box - List from Hidden range - VBA worng
    ... In your for loop, ... range is only 1 column wide this will be a single cell). ... Basic Editor type the word on it's own in lower case on a line and hit enter. ... in the array reference - and an array starts with element 0. ...
    (microsoft.public.excel.programming)
  • Re: Wrong data type returned from user-defined function
    ... If you want it to function as an array function then it needs to be ... Dim strFormula As String ... UnRound = CVErr ... would execute just as if I'd typed it in the cell myself. ...
    (microsoft.public.excel.programming)