Hey fellow programmers,
Firstly, i want to explain the purpose of my task, which is to create an interactable UI grid where the user can select whatever grid element he is interested in. (This is already done, using Grid Layout Group - see attached image)
The whole grid is supposed to represent a real-life-sized squared area, which I have from data, consisting of length and width. From data, I also have an event list, which contains certain real-life events and positional data (x,y) of each event. So when a user selects a grid elemental, I want it to represent all events that is equal to within that positional range.
My idea: is so far is to find a calculation method to get the size of the UI grid relative to the real life size. But i need a way to actually get the size of the grid (Not hardcoding). And then access and sort each child element, so if the element which has been clicked on the attached image, would equal to (0,0), (0,1) and (0,2), so i can loop through each child element
I know this is a messy explanation, but I hope it makes somekind of sense.
I guess you need the following calculations:
Given the TotalWidth, TotalHeight and CellWidth, CellHeight of your grid and a Position within TotalWidth/TotalHeight. Where TotalWidth is the X-Width of your grid, CellWidth the X-Width of your cells, Position the position within your grid.
CellIndexX = Position.X % (TotalWidth / CellWidth);
CellIndexY = Position.Y / (TotalHeight / CellHeight);
TotalWidth / CellWidth basically calculates the amount of Cells on the X-Axis, and the same happens for the amount of cells for the Y-Axis.
This should then allow you to get the cell by Index like:
int TotalWidth = 4000;
int TotalHeight = 2000;
int CellWidth = 20; // TotalWidth / CellWidth = 4000/20 = 200 grid tiles on the x-Axis
int CellHeight = 20; // TotalWidth / CellWidth = 4000/20 = 100 grid tiles on the y-Axis
Cell[,] myGrid;
Cell GetCellForPosition(Vector3 Position)
{
CellIndexX = Position.X % (TotalWidth / CellWidth);
CellIndexY = Position.Y / (TotalHeight / CellHeight);
Cell result = myGrid[CellIndexY, CellIndexX];
return result;
}
For the real world size versus the grid size, you can use the rule of three. Or more simplified, you just come up with a factor
If the RealWorldSize is: 120X meters * 90Y meters
and you want the grid to be 100X meters, your factor would be: 100/120 = 0.8333, which you can then use to calculate the Y height of your grid by: YGridHeight = 90 * 0.833 = 75;
Related
im currently working on a game and i need to divide a changeable amount of objects in to a limited area. I made a image to explain better:
(I know its a bit difficult to understand)
So i can't find a solution and couldn't figure it out myself. I tried a few ways to multiply and divide the object count with the index and the xMaximum but it didn't work as expected.
This is what i tried:
public float xMin, xMax;
private void UpdateValues()
{
for(int i = 0; i < transform.childCount; i++)
{
float value = xMin;
float maxIndex = transform.childCount;
float index = i;
value = transform.position.x + xMax / (maxIndex * index);
transform.GetChild(i).transform.position = new Vector3(transform.position.x, transform.position.y, value);
}
}
It just gave me the wrong numbers and i can't figure the right way out.
The available variables are:
xMinimum, xMaximum
objectcount
objectindex
If you want it to look exactly how you have shown it on images then it might be a little more tricky. Objects don’t fill the area always the same, so we need to add some exceptions:
if there is only 1 item, then just set it’s position to (xMin + xMax) / 2f
if item is first, and there are more than one, set its position to xMin + (squareWidth / 2f) (or something else if pivot is not in the middle)
if item is last and there are more than one, set its position to xMax - (squareWidth / 2f)
the remaining area, which is of size xMax - 2 * squareWidth need to be divided equally between all remaining objects. Compute the amount remaining objects (which will be all objects - 2 (first and last)). Divide remaining area by number of objects plus 1. Set remaining objects position in a loop, the statement inside will look something like this: value = (i+1) * remainingArea / (transform.childCount - 2 + 1)
That’s the rough list of steps that might help. In this example I assume, that xMin is 0, otherwise you will need to take that into account and for eg. Remaining area won’t be just xMax - 2 * squareWidth, but xMax - xMin - 2 * squareWidth.
Also you might find yourself being off a half a square here and there, but that should be easy to correct.
In order to have the same scale on both axes, X and Y, I used PlotType.Cartesian which ensures that:
_model = new PlotModel();
_model.PlotType = PlotType.Cartesian;
I also have possibility to zoom in and out charts.
In order to control zooming I need to set AbsoluteMinimum and AbsoluteMaximum on both axes and specify minimum and maximum range.
Issues I have: how to keep the same scale when zooming? Because axes are zooming independently and often one axis is getting out of sync with the other axis (when one reaches its limits and the other still can expand).
Also, how to set appropriate values for both axes, because if I set all minimums and maximums, I expected correpsonding values on the other axes to be set, if I use PlotType.Cartesian, but it does not happen - this is the reason the issue arises, because i can't set appropriate values for both axes.
The closest I could get is:
subscribe to Loaded event of PlotView (_model field in this case)
in that method get ActualHeight and ActualWidth of PlotView
Having size of plot area, one can choose min and max of desired axe, then do all calculations required to keep scale the same on both axes. For example:
double xMin = -500;
double xMax = 800;
double xRange = xMax - xMin;
double yRange = xRange / ActualWidth * ActualHeight;
double yMin = 58;
double yMax = yMin + yRange;
_model.Axes[0].Minimum = xMin;
_model.Axes[0].AbsoluteMinimum = xMin;
_model.Axes[0].Maximum = xMax;
_model.Axes[0].AbsoluteMaximum = xMax;
// Analogically, define limits of Y axe
Also it is important to zoom both axes with the same zooming factor!
This will guarantee equal scales on both axes and keeping aspect ration through zooming.
uI am making a program to turn an image into coloured 0's, the problem is that the 0's are not colouring properly. To get anything near resembling the image I have to start my for loop at 2 and increase by 3 each time. The following is my current code:
public partial class MainWindow : Window
{
public MainWindow()
{
TextSelection textRange;
TextPointer start;
TextPointer startPos;
TextPointer endPos;
System.Drawing.Color x;
int pixelX = 3;
int pixelY = 8;
InitializeComponent();
Bitmap b = new Bitmap(#"E:\Documents\Visual Studio 2015\Projects\RichTextBox Image to ASCII\RichTextBox Image to ASCII\Akarin.jpg");
for (int i = 2; i < 8000; i += 3)
{
textRange = richTextBox1.Selection;
start = richTextBox1.Document.ContentStart;
startPos = start.GetPositionAtOffset(i);
endPos = start.GetPositionAtOffset(i + 1);
textRange.Select(startPos, endPos);
x = b.GetPixel(pixelX, pixelY);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(System.Windows.Media.Color.FromArgb(x.A, x.R, x.G, x.B)));
pixelX += 6;
if (pixelX > 1267)
{
pixelX = 3;
pixelY += 16;
}
i += 3;
textRange = richTextBox1.Selection;
start = richTextBox1.Document.ContentStart;
startPos = start.GetPositionAtOffset(i);
endPos = start.GetPositionAtOffset(i + 1);
textRange.Select(startPos, endPos);
x = b.GetPixel(pixelX, pixelY);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(System.Windows.Media.Color.FromArgb(x.A, x.R, x.G, x.B)));
pixelX += 7;
if (pixelX > 1267)
{
pixelX = 3;
pixelY += 16;
}
}
}
}
The reason that I am putting the code in the for loop twice is because when you take the amount of 0's that fit horizontally and find out how many pixels each 0 takes up, it comes to about 6.5 because of the space between each 0.
EDIT: Something else that is also strange, if you look in the top left corner where it starts colouring the 0's, 4 in a row are properly coloured, but then the rest are coloured every other.
A few serious problems I'm seeing here. Normally when rasterizing you either loop through the source pixels or through the target pixels. You however... you loop by a fixed value of roughly 2666 ((8000 - 2) / 3). It's also a very bad idea to do things twice in a loop and even change the loop variable (i). Furthermore since you're having only one loop you have to care about both axes in one run. This is very error prone.
How about this approach?:
Your source image is 1280 × 720 square pixels
Since your zeros are not square you have to know their aspect ratio. If you know that you can calculate how many rows and columns you need. You probably don't want to match them 1:1 as this would give you a huge and stretched image.
Once you know how many rows and columns you need, do two loops, one inside the other and call the loop variables targetX and targetY
If your target image is supposed to be let's say 400 zeroes long in the x-axis, make the first loop go from 1 to 400
Inside the loop pick one pixel (color) from the source at 1280/400 * targetX. Your first target pixel would be at x position 1280/400 * 1 = 3,2 which is roughly 3 (round the number after calculating it). The second would be 1280/400 * 2 = 6 and so on. I think this is the biggest pain in your algorithm since you're trying to get around the 6,5px width. Just round it after calculating! If the first is 6,5, make it 7, the second is 13... you get the idea.
Same logic goes for Y axis, but you handle this with targetY.
I am currently implementing Pan/Tilt/Zoom support in my application. I pass in an image and then calculate zoom in the following manner
int widthPercent = width / 100;
int heightPercent = height / 100;
int zoomX = width - (widthPercent * (int)Zoom);
int zoomY = height - (heightPercent * (int)Zoom);
where width is width of original image, height is height of original image and Zoom is a value passed in from the UI, ranging from 0 to 100.
I now wish to implement Pan/Tilt support while an image is zoomed in so that the whole image can still be accessed. Again Pan and Tilt will be controlled from the UI(again 0 to 100) but I want it to stay within the boundaries of the image so that the image does not repeat which it is currently doing.
I am current calculating the Pan and Tilt like so:
// Calculate Pan
int panWidthPercent = width / 100;
int finalPan = (int)Pan * panWidthPercent;
// Calculate Tilt
int tiltHeightPercent = height / 100;
int finalTilt = (int)Tilt * tiltHeightPercent;
This works to an extent however it seems to keep repeating the image after panning for a small time(usually when Pan = 10 or more). I wish it to stop after it renders all the image i.e it reaches the width but adding something like the following doesnt seem to stop this
if(finalPan >= width) finalPan = width;
Once the values are calculate I create a source rectangle using these values
mClippingRectangle = new Int32Rect(finalPan, finalTilt, zoomX, zoomY);
which is then used against the base image for rendering only that rectangle
In short how can I calculate how much I should pan/tilt when I already know the zoom of the image
Managed to accomplish it by checking whether the Pan/Tilt Amount added to the zoom width/height is greater than the overall image width/height then stop it from panning/tilting
// Calculate Pan
double finalPan = Pan * widthPercent;
if ((finalPan + zoomX) >= width)
{
finalPan = width - zoomX;
}
// Calculate Tilt
double finalTilt = Tilt * heightPercent;
if ((finalTilt + zoomY) >= height)
{
finalTilt = height - zoomY;
}
I'm developing an application to manipulate images scanned on a wide-image scanner. These images are shown as a ImageBrush on a Canvas.
On this Canvas they can a make Rectangle with the mouse, to define an area to be cropped.
My problem here is to resize the Rectangle according to the original image size, so that it crops the exact area on the original image.
I've tried many things so far and it's just sqeezing my brain, to figure out the right solution.
I know that I need to get the percent that the original image is bigger than the image shown on the canvas.
The dimentions of the original image are:
h: 5606
w: 7677
And when I show the image, they are:
h: 1058,04
w: 1910
Which gives these numbers:
float percentWidth = ((originalWidth - resizedWidth) / originalWidth) * 100;
float percentHeight = ((originalHeight - resizedHeight) / originalHeight) * 100;
percentWidth = 75,12049
percentHeight = 81,12665
From here I can't figure how to resize the Rectangle correctly, to fit the original image.
My last approach was this:
int newRectWidth = (int)((originalWidth * percentWidth) / 100);
int newRectHeight = (int)((originalHeight * percentHeight) / 100);
int newRectX = (int)(rectX + ((rectX * percentWidth) / 100));
int newRectY = (int)(rectY + ((rectY * percentHeight) / 100));
Hopefully someone can lead me in the right direction, because i'm off track here and I can't see what i'm missing.
Solution
private System.Drawing.Rectangle FitRectangleToOriginal(
float resizedWidth,
float resizedHeight,
float originalWidth,
float originalHeight,
float rectWidth,
float rectHeight,
double rectX,
double rectY)
{
// Calculate the ratio between original and resized image
float ratioWidth = originalWidth / resizedWidth;
float ratioHeight = originalHeight / resizedHeight;
// create a new rectagle, by resizing the old values
// by the ratio calculated above
int newRectWidth = (int)(rectWidth * ratioWidth);
int newRectHeight = (int)(rectHeight * ratioHeight);
int newRectX = (int)(rectX * ratioWidth);
int newRectY = (int)(rectY * ratioHeight);
return new System.Drawing.Rectangle(newRectX, newRectY, newRectWidth, newRectHeight);
}
I think the only reliable option is to let your users zoom in to the image (100% or higher zoom level) and make a selection on part of the image. This way they can make an exact pixel-based selection. (Assuming that the purpose of your selection rectangle is to select part of an image.)
Your problem now is that you're using floating-point calculations because of the 75% zoom level and rounding errors will make your selection rectangles inaccurate. No matter what you do, when you try to make a selection on a shrinked image, you're not selecting exact pixels - you're selecting parts of pixels as you resize your rectangle. Since a partial pixel cannot be selected, the selection edges will be rounded up or down so you either select one pixel too many or one pixel too few in a given direction.
Another issue that I just noticed is that you distort your image - horizontally it's 75% zoom, vertically it's 81%. This makes it even harder for users because the image will be smoothed differently in the two directions. Horizontally 4 original pixels will be interpolated on 3 output pixels; vertically 5 original pixels will be interpolated on 4 output pixels.
You are actually doing a form of projection. Don't use percentages, just use the ratio between 5606 and 1058,4 = ~5.30. When the user drags the rectangle, reproject it which is selectedWidth * 5606/1058.4.