In visual basic, is it possible to teleport my cursor to x*y coordinates (Not x,y) - vb.net

So I have this program in visual basic that requires teleporting your cursor around and clicking. Kinda like an automatic cheat for a game but not really a cheat.
Anyways I want the user to set coordinates to a point, then set those coordinates to a value, then with the push of a button teleport to those coordinates.
Alright I can make him get the coordinates. I can make the cursor teleport. I can set the coordinates he got to an integer X and an integer Y so the cursor teleports there. But I have to do all that 8 times. So I need 24 integers.
I was thinking, maybe I can skip all that if I can multiply xy so lets say he sets the coordinates to 320 (x) and 72 (y). (320,72) Alright so If I multiply xy I get the pixel number on the screen that exists in that coordinate (right?)
Alright that makes it so we only have 8 integers which is much less.
But how do I make the cursor teleport to the location "X*Y" (in the case of 320*72 it is pixel number 23040).
*TL;DR is it possible to convert this Windows.Forms.Cursor.Position = New Point(320,72) into this Windows.Forms.Cursor.Position = New Point(23040 (which is 320*72))*
Is it possible?
I can't think of a way of doing it please help me. Thanks.

No. (x, y) is not equivalent to (x * y).
The reason why is obvious if you give it some thought. There are many different input values for x and y that would give the same product. For example, 6 * 1 = 6, and so does 2 * 3. Which location on the screen would 6 correspond to? The point at coordinates (6, 1), (1, 6), (2, 3), or (3, 2)?
There might be a better way of doing this, but I find the description in your question to be very difficult to understand. If all you want to do is simulate a click event at a particular location on the screen, you do not need to explicitly move the cursor there first. I'd suggest how to do that, but it is not clear what language you're using. You've tagged the question [vbscript], but then talked about Windows Forms, which doesn't exist in VBScript. If you are using VB.NET, you will probably end up P/Invoking the SendInput function. Google for code examples.

While I agree with other answers/comments, what you describe is possible, but you cannot simply multiply the x and y coordinates. You need to take the screen width into consideration:
Private Function PointToPixelNumber(pt As Point) As Integer
Return pt.Y * Screen.PrimaryScreen.Bounds.Width + pt.X
End Function
Private Function PixelNumberToPoint(pixelNumber As Integer) As Point
Return New Point(pixelNumber \ Screen.PrimaryScreen.Bounds.Width, pixelNumber Mod Screen.PrimaryScreen.Bounds.Width)
End Function
Please note that this code uses integer division (\). It also assumes that the origin is at the upper left of the screen and is zero based (i. e. the upper left coordinate is (0, 0))

Related

Change point location

I want, when a point is selected, the co ordinate values of the point to display in a userform:
Values of x, y & z should be displayed in the textboxes.
Some points are in "ONCURVE" or "ONSURFACE" type. Those points won't show x, y & z values.
Image for reference
First point is co-ordinate defined, second point is ONCurve defined and the third is ONSURFACE defined.
How do I extract x y z co-ordinate values for those points?
I also need to change the point location by changing the values from the textbox.
As GisMofx said, you should use the base Point class and you can use the methods GetCoordinates
Take a look in the API documentation - you should be able to find some examples and if not try to come up with something
CATIA API Point Description

2-dimensional bin-packing in Excel

I’m trying to find a solution for the 2-dimensional packing problem using Excel (Formulas, Solver, VBA).
But apart from finding said solution, I would like to bring back this topic as base for discussion, because I realized during my extended web-searches that this problem (or variations of it) creates headaches for many people – novice and professional users.
The explanation for my problem:
I am trying to fit rectangular packages in rectangular containers. Usually there is one larger box and 2-5 smaller boxes to ship.
On average, there is still capacity of 30-50% left in the containers, so I want to calculate how many additional standardized boxes would fit in this free space to fill up the container.
There are no constraints, as long as the boxes fit into the container.
Height and weight are irrelevant.
The Boxes can be rotated by 90°.
One 40’ container is 1203cm long and 233cm wide.
The standardized boxes are 85cm x 70cm
The other boxes have different sizes.
I checked bin-packing algorithms but as of now I was not able to implement any solution in excel. I’d prefer a way to calculate this using Excel Solver or VBA, but my VBA-programming knowledge is limited.
The knapsack problem does not apply here in my opinion, although it is mentioned many times in this context.
In my case, I would be happy with a solution giving me something like: “You can fit at least x additional Boxes in the container”. Some inaccuracy does not matter – meaning up to 25% less boxes than possible. Too much boxes, on the other hand, are a no-go.
Now, do you guys have any idea how to get started here or even accomplish this? Maybe there is even a super-simple approximation I don’t know of?
Thanks!
UPDATE
After quite some time, I finally found some hours to get into this problem again.
I read Erwin Kalvelagen ‘s Blogposts and some papers on bin packing algorithms.
Also, the solver option is off the table.
I decided to go for a Bottom-Left-Algorithm (BLT) with some restraints (not just greedy).
Quick explanation of the BLT-Algorithm: Each box is placed in the bottom-most and left-most possible position in a given area (container). When a box is placed, it creates two new “corners” where the remaining boxes can be placed. Initially, the boxes are sorted by length (to start with the longest box) and place them in a 2-dimensional array. Then the starting point will be set in an Array (x, y coordinates) – the first coordinates are obviously 0, 0 as we start in an empty container. Then the algorithm would try to place the first box in the bottom-left corner with coordinates 0, 0 – which of course works perfectly. Then the starting cords would be replaced by the coords of one of the new corners and the coords of the other corner will be added to C. this would loop until all non-standard boxes are loaded. Then the algorithm would add standardized boxes if possible (and count them). The loop would end, if adding more boxes is not possible anymore due to constraints.
The dimension of the non-standard boxes will be entered in a worksheet - one box per row. The dimensions of the container and the standardized boxes will be written there as well.
Constraints would be, that no box can overlap another and all boxes would have to inside the container. Although rotation is practically possible, it is not necessary to implement it in the code as I am trying to orient the packages along the container.
Here is some pseudo code of the BLT-Algorithm I found:
**Procedure BLF(width, height,maxWidth)**
begin
initialize the arrays x and y
initialize the list and add the null point
for all rectangles
initialize choosePoint as impossible
while choosePoint is impossible and j < length of list
if the rectangle could be placed in a specific point
choose the point
endif
endwhile
if choosePoint is possible
update the arrays x and y
remove the point from the position choosePoint
from list
add the points (xi+width,yi),(xi,yi+height) to the points list
else
if (width > maxWidth) the problem has no solution
else xi = 0 and yi = max(heightk + yk)
where k 2 {1, . . . , i − 1}
endif
endif
endfor
solutions: the arrays x and y with (xi, yi)
the coordinates of rectangle i
end
Now, although I know a lot (like really A LOT) more about packing algorithms I am still not very experienced with VBA. Especially not with implementing algorithms.
So again I would be happy for any help you can give me to get started with the implementation.
So I started off with this (I know it’s really nothing, but I find it quite difficult):
Sub BLT1()
Dim Boxes As Variant, i As Integer, j As Integer ‘’Boxes dimensions
Dim Cntnr As Variant, a As Integer, b As Integer ‘’Container dimensions
Dim BLPoints As Variant ‘’Array with coordinates of bottom-left corners
Boxes = Range("B11:C15")
Cntnr = Range("D2:E2")
‘’Now I would like to add the first coordinates (0, 0) to the BLPoints
‘’Then I want to pick the first box and fit it in the container at the (0, 0) coordinates
‘’Then I want to update the BLPoints array with the new coordinates
…
End Sub
I’m looking forward to any constructive feedback and advice!
This is not a very easy problem. Some possible approaches are:
A MIP (Mixed Integer Programming) Model. The most complex part are the no-overlap constraint. For each box in the container we need to make sure it does not occupy space used by another box. The MIP approach has the advantage that we can find optimal solutions, or very good solutions with an indication how much we are away from a possible best solution (i.e. an indication of the quality of the solution).
A constraint programming model. Similar to the MIP model, but some constructs are easier to handle (i.e. the OR construct needed to formulate the no-overlap constraints).
A heuristic or meta-heuristic approach.
I implemented quickly a MIP model and it turns out you can get optimal or near-optimal solutions quite quickly. The solution below was found in less than a minute using a commercial MIP solver:
The yellow boxes are the required non-standard boxes and the blue ones are the optional standard boxes.
See here for more information about these no-overlap constraints. Here are the no-overlap constraints for this problem.

Can't use all arguments with Shapes to control line in the GraphicsWindow

After trying a few things inside small basic to make a line follow the mouse but not move the entire line, I recently came across a problem. Originally I was trying to constantly have a line update so as it stays connected from one point to the mouse position by clearing the graphics window and redrawing a line from the bottom right to the mouse. This could not work and was to resource intensive. However, now I have come across Shape.addline and shape.move. But I'm not too sure as to how they work, from my understanding, a shape can have it's own name by doing:
[Shapename] = Shapes.addline(positions)
and then that shape can be moved using:
Shapes.move(Shapename,Coordinates)
In my case it's:
L1 = Shapes.AddLine(0,GraphicsWindow.Height,GraphicsWindow.MouseX,GraphicsWindow.MouseY)
(Drawing a line from the bottom left corner to the mouses position)
and
Shapes.Move(L1,GraphicsWindow.MouseX,GraphicsWindow.MouseY)
The only problem is that Shapes.Move only supports 3 arguments being:
shapeName
X
Y
But, when drawing the line (Shapes.AddLine), I use 4 arguments:
X1
Y1
X2
Y2
This means I can only control those two positions. So, how would you control the other two? If we can only modify X1 and Y1, is there any way of still using at least something similar to the shape.move method but be able to control the other X2 and Y2 positions? Primarily, I would like to actually Only change the X2 and Y2 positions, as I'm trying to make a line originate from one point and stay there, then alter the opposing point so that it follows the mouse, and not move the entire shape. If none of this is possible, is there any known way of moving / changing only the X2 and Y2 coordinates of a line without having to clear the entire screen?
Ah yes. These are the shortcomings of small basic. Shapes.move will not let you define a starting and ending point of a line. What you will need to do is move the center of the line in between the first point and the cursor, and the rotate it correctly. Like so:
Mouseline = Shapes.AddLine(0,0,100,0)
Shapes.Move(Mouseline,200,200)
GraphicsWindow.MouseMove = OnMouseMove
Sub OnMouseMove
XDif = (GraphicsWindow.MouseX-250)
YDif = (GraphicsWindow.MouseY-200)
If XDif <> 0 Then
MouseAngle = Math.ArcTan(YDif/XDif)
EndIf
If XDif < 0 Then
MouseAngle = MouseAngle + 3.14 '180 degrees in radians
EndIf
Shapes.Rotate(Mouseline,Math.GetDegrees(MouseAngle))
Shapes.Move(Mouseline,(Math.Cos(MouseAngle)*50)+200,(Math.Sin(MouseAngle)*50)+200)
EndSub
Another way of doing this is with the LitDev extension (http://litdev.co.uk/). It has a MoveLine(x1,y1,x2,y2) function in it.
im guessing u would alter the end of the program where it says math.cos(mouseangle) change the 200 to 0 and change the other 200 to the bottom. so if what im trying to figure out, ur trying to get the line to only project in the 1st quadrant in a cortesian plane yes?

How do I avoid the first(and last) column of a x-capped chart being truncated?

I've been fiddling with a chart... I set the X axis minimum value to zero. Then I used DataBindXY on the Chart.Series.First.Points to set the values passing two lists (first is a 0-based label list and other has the actual values).
Below is the result. As I highlighted by the red arrow the first column is truncated.
Microsoft made traversing the settings of a chart in the designer as simple as getting out of a maze, so I'm clueless about where to find some offset property to be set.
Edit:
Mine could be a possible duplicate of this question, but its answer is not clear to me, so I asked a new one.
If you set the X axis minimum value to -0.5, that should solve it.
GraphChart.ChartAreas[0].AxisX.Minimum = -0.5;
or try something like:
GraphChart.ChartAreas[0].AxisX.IntervalOffset = 0.5;
The reason is that the columns width is approximately 0.8 depending on the chart configuration so in your case they might span from 0.4 to 1.4, with a center point in 1

Visual Basic 6 Dynamic Variables

I am doing a program using vb6 which is stored datas from mouseclick in term of coordinates. I succeeded doing the first stage which is displaying the clicked coordinates. My problem now is I need to save the coordinate in term of variables so i can call them back to use for another purpose for example to find distance between two points.
if its just two coordinates its easier to find the distance. but when it comes to many coordinates I am stuck. I tried to do an array to stored the data inside loop
1. InputX(ListNum, 0) = Int(x)
2. InputY(ListNum, 1) = Int(y)
3. ListNum=ListNum+1
when I try to call for InputX(2,0) = Text1.Text or Text1.Text=InputX(2,0) none of them working. It seems that the data will be erased after I do a mouseclick
Is there any way which I can set the dynamic variables which stored each my clicked coordinates such as Input1,Input2,Input3 ...InputN
I do this in VB6.
The problem you're having is that you're using a two dimensional array there. A two dimensional array looks like a table. That's not what you want though. You want a list of pairs of points. So, create a structure with two integers in it, x and y, and make an array of those structures:
'Right underneath your Class Form1 declaration:
Structure Point
Dim x As Integer
Dim y As Integer
End Structure
Dim length As Integer = 10
Dim Points(length) As Point
'When you want to start using your points put this in the method:
Points(0).x = 10
Points(0).y = 10
Points(1).x = 20
Points(1).y = 40
Dynamic variables in VB6
First you declare the variable without giving size:
Dim InputX() As String
Then you give for the first time size to your array using ReDim:
ReDim InputX(5)
If you want to preserve whatever data is already in your array you use ReDim Preserve:
ReDim Preserve InputX(10)
I hope this is what you need.
it appears that the first method
Text1.Text=InputX(2,0)
is working. I just need to declare x and y As Single