Collision of two diffrent object on GDI [closed] - c#

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

Related

How to traverse through char array [closed]

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.)

Make "object" delete at "distance", when "created"? C# programming [closed]

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).

Determine if image is square [closed]

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.

WPF FlowDocument layout [closed]

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.

c# get property of element in list with mixed types [closed]

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 have a list with: Circle, Triangle, Rectangle in it
i want to edit the element with the id X but list[X].radius; is not available because it is a child class.
You will have to detect the dynamic type of the element at runtime.
IShape value = list[x];
if(value is Circle)
{
((Circle)value).radius = 5;
}
You can also do something like:
Circle value = list[x] as Circle;
if(value != null)
{
value.radius = 5;
}
This has the advantage of being a bit faster, since the cast is only done once.
When you have a mixed list and want to access members defined for the derived types, you have to cast to the derived type.
((Circle)list[index]).Radius = 10; // alternately use is or as if you're unsure
Of course, by the virtue of simply having a mixed list, you're saying that you generally do not care about the differences between the derived types, you're content with using the base polymorphically. If you find yourself in a different position, you should perhaps rethink your strategy for storing or consuming these elements.
Typecast it:
((Circle)list[X]).Radius

Categories