[ad_1]
I’m making a easy recreation in C# utilizing pictureboxes. I wish to transfer a picturebox on a kind. As soon as this picturebox is positioned in a brand new location, I normalize its X,Y coordinates between [0, 1] after which ship them to a server. The server does nothing however broadcast these normalized coordinates to all the related shoppers.
As soon as a consumer will get the normalized coordinates from the server, it interprets them again into display screen coordinates so it is aware of the place to put the picturebox. This enables the picturebox to show within the right spot for each screens, even when the display screen sizes are very completely different.
Right here is the code that exhibits how this course of is completed: https://pastebin.com/1igRRtKJ
If each screens are the very same dimension (particularly if the shape sizes I’m testing with are the identical) then this course of works completely. Nonetheless, if one display screen dimension is completely different than the opposite (particularly, the shape sizes are drastically completely different) then they do not show fairly proper.
If one picture is positioned to be subsequent to a different on the small display screen, the massive display screen will present a ton of spacing between them that should not exist. Likewise, if the massive display screen strikes a picture to take a seat subsequent to a different one, the small display screen will present them overlapping one another which mustn’t occur.
Listed below are some screenshots illustrating the issue: https://imgur.com/a/y2NTHm3 and https://imgur.com/a/oIV5Huy
Given the code I shared and the issue I’m describing, how can I repair this problem? I might just like the pictureboxes to point out up in the very same place on all the consumer’s screens, even when they’re vastly completely different from one another.
I am pretty sure the mathematics right here is right. So, I’m uncertain what the precise drawback is. When the display screen sizes are completely different the photographs will seem roughly the place they need to go, however not precisely. I might wish to one way or the other keep the relative distance, which I believed this may do, however clearly I’m lacking one thing. I set the pictureboxes utilizing the placement technique, which relies on the top-left nook of the picturebox.
Normalize coordinates (consumer sending the normalized coordinates of the moved picture):
normalizedX = picturebox.location.x / (client_width - picturebox.width);
normalizedY = picturebox.location.y / (client_height - picturebox.top);
Translate coordinates (shoppers getting the normalized coordinates):
adjustedX = normalizedX * (client_width - picturebox.width);
adjustedY = normalizedY * (client_height - picturebox.top);
I might actually love to unravel this drawback. I’ve requested this query in a number of completely different locations, however nobody appears to have the ability to assist.
Edit: Here’s a hyperlink to an instance challenge that condenses this drawback into a really compact, however practical instance in case anybody wish to troubleshoot this fashion. https://ufile.io/pwqig1n9
The code in that file is a tiny bit completely different than what I shared, however should not make any distinction relating to this query as a result of each variations have the very same problem and normalization/translation code. The one distinction is the challenge I linked can transfer two completely different photographs round as a substitute of simply the one.
Edit2:
Since making this put up I’ve discovered that the issue is that the photographs should even be scaled for this resolution to work as anticipated. Nonetheless, I’ve been unable to get this to work.
I used the default kind dimension as a reference dimension (600,600). When the shape is resized I’ve tried calculating a scaling issue utilizing the brand new kind dimension and the reference kind dimension like so:
scaleFactor = Math.Max(client_width / reference_width, client_height / reference.top)
The purpose of taking the max of those is to try to keep the side ratio of the picturebox. I am unsure if that is precisely method this, however that is what I’ve tried.
As soon as I’ve the size issue I exploit it to scale the pictureboxes on the display screen when the shape is resized:
picturebox.width = picturebox.width * scaleFactor;
picturebox.top = picturebox.top * scaleFactor;
I do not know if that is right, however the picturebox does scale. I’m simply unsure whether it is scaling correctly.
Nonetheless, I nonetheless have the issue of the pictureboxes not being correctly proven (place and distance between one another) when the types are a special dimension.
Might anybody please use the unique code instance I’ve supplied and present me how this needs to be applied? I simply can’t appear to get it to work.
[ad_2]