It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Is there a quick method to determine if an image is a square or not in C#?
Question has been phrased incorrectly, my apologies.
Is there a way to determine if an image can be scaled "down" to fit into a square block, without cropping either height or width, for example, if I have 960x640, we have a square, on it's width, but if we have 640x960, we don't.
I need to be able to determine if an image can be scaled down 100% into a square block, for example, 150x150, without losing portions of the image.
Update
Let me try again.
I have to iterate a collection of images:
960x658
960x566
960x381
960x378
714x960
658x960
I know, that the first two images will be square (150x150), I know the middle two will be rectangular (horizontal) (300x150) and I know the remaining two will be rectangular (vertical) (150x300). Is there an algorithm, 3rd party component or built in method to determine this for me?
I don't want to go and code nested spaghetti code using if statements to do this? I'm so lost :$
Is there a quick method to determine if an image is a square or not in C#?
Well you're pretty much giving the answer yourself. You have the image. You have the image's properties.
if(img.Width == img.Height)
//I'm a square
Now since you're iterating through a collection of images.
foreach(Image img in myImageCollection)
if(img.Width == img.Height)
squareImages.Add(img);
As simple as that.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Function receives char[,].
For example if it it takes
000
LAD
0B0
Traversing should print out all possible combinations of non-zero chars:
L
LA
LAD
LAB
A
AL
AB
AD
and so on
private void Traverse(char[,] area)
{
}
The easiest way is to write a recursive function with two strings, initial and output. I assume you want combinations, not permutation so it's a little easier. The base case is checking if initial is empty, then print out the output. The step is removing a character from initial and calling the recursive function twice, one with the unchanged output and one with the removed character added into the output. If the removed character is 0, however, then you only call the function once (removing the 0 without adding anything to the output.)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm a complete newb, working on a 2D RTS game. So far I've created an object "infantry" and can make it shoot a "bullet" in specified direction, and can only find the code to delete bullet once outside the room. I want the bullet to delete at a relative distance of 300pixels from the infantry. And need the code to be very clean, hence there will be a lot of bullets eventually. My question is: Should I make a rectangle around the infantry, to specify range and line of sight, and when bullet collides with range = delete? or Should the bullet class say "once created + distance traveled = 300 pixels = delete"?... Any code, or hints you can offer would be greatly appreciated.
Simple method:
Store the origin point, where the projectile is created (at the gun barrel)
Each frame, use the distance formula to find out whether the projectile has exceeded the travel radius.
You can avoid the Math.Sqrt call in the distance formula, by comparing to the square of the radius.
var dx = xbullet - xorigin;
var dy = ybullet - yorigin;
if (dx*dx + dy*dy > range*range) delete bullet
I think your second suggestion is better. If you make a box or check the distance to the soldier, then the bullet could travel longer if the soldier run after the bullet (if the bullet is not instant-travel, that is).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
H iguys. I have a loop which parse users id's and adds tham to txt file.
What's the best way than to check if this txt has this id skip it ( while next parsing ).
The size of txt rises from 5-..... mb
I tried to add ids to List, but when the size of file if bigger than 5mb the app begins hanging
Use a HashSet<int> or HashSet<string>, collect the ids in it, then at the end write the result to the text file.
PS: Note that HashSet is O(1) while List is O(n)
You should probably load all of the IDs in the text file into some collection and check if that collection contains the IDs.
I honestly don't think that there's a much more efficient way of doing it than that.
A rule of thumb I believe is to trade time with space. If you want to make copying faster and avoid looking into the file again and again then you may maintain an array or linked list or hash table which also have the id stored in it
var userIsAlreadyThere = File.ReadLines(path).Contains(userid);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a FlowDocument in which I need to insert many fragments of dynamic text at arbitrary positions.
For example, I need to place text "Hello" at x = 10, y = 15 and text "World" at x = 10, y = 20.
I'm currently doing this using Figure. I create a Run with the text, put it in a Paragraph, and put all that in a Figure. From there I can set it's VerticalOffset and HorizontalOffset. Then I place all the figures in a Paragraph which I add to the FlowDocument.
Everything was going OK (it was the simplest approach I could came up) until I had to place two or three fragments of text on the same line (at the same y but different x).
For some reason it's putting each Figure on a new line instead of putting them all in the same line, let me illustrate:
Expected:
text1 text2 text3
Actual:
text1 text2 text3
Does anybody know how to remove that line break between figures? If you have a better approach to this problem I'm open to suggestions too.
I ditched FlowDocument and went with XAML and used a Canvas... made my life much easier being able to place all text fields visually... much easier to maintain in the long run too.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to create a simple game .
think I have two uneven ( not systematical ) GDI object in one Graphics , user move object one , and I want to be notified when object one has Collision with object two , how can I do this with C# ?
Draw both the objects black on a single bitmap. Then draw one of them on a separate bitmap. Then subtract one bitmap from the other. If the result is same as the bitmap with the other object drawn on a separate bitmap then you have no collision otherwise you have a collision.
This doesn't require any math and works with objects of irregular shape. Just bitmap difference and sum operation.
Sudo code
var bmpObject1 = new bitmap->draw object 1
var bmpObject2 = new bitmap>draw object 2
var bmpCombined = new bitmap>draw object 1 and 2
if (bmpObject1 = (bmpCombined - bmpObject2))
no collision
else
collision