I was actually working on one of these. The problem is mainly with vertical lines; those won't work with your equations.
Here's a challenge: can you check if a point is in a triangle, given xy coordinates for all three vertices of each triangle, and the xy coordinate of the point?
Offline
Create three vectors that connect the given point with the three vertices of the triangle. Look at the angles between adjacent vectors (you can use the formula for calculating the angle between vectors, can't remember it right now). If those angles don't add up to 360, the point is outside the triangle.
Last edited by amcerbu (2012-05-21 23:03:11)
Offline
So, given triangle with points at
(x1, y1), (x2, y2), and (x3, y3),
and some random point (x, y),
the vectors are
v1 = (x1 - x, y1 - y)
v2 = (x2 - x, y2 - y)
v3 = (x3 - x, y3 - y)
By the definition of dot product, a . b = |a| * |b| * cos(θ), where θ is the angle between them, |a| indicates the magnitude of a, and (a . b) is the dot product of a and b. Therefore,
θ = arccos((a . b) / (|a| * |b|))
Since we're dealing with a triangle, every point is adjacent to every other point, so the order of the points doesn't matter. We know that v1 and v2 form an angle, v2 and v3 form an angle, and v3 and v1 form an angle.
So, we calculate the angles between those three sets of vectors, and add up. The "angle between vectors" function always returns an angle less than or equal to 180 degrees. If the point is outside of the circle, you'll end up with a sum of angles less than 360 degrees.
Last edited by amcerbu (2012-05-22 16:37:07)
Offline
... I knew I could do something like that, but I wanted to make an equasion-thingy. I haven't learned about dot products yet, and I thought that if you took the aveage of all the perimeters of all the triangles that the point made when lines were drawn from each vertici of the triangle to it, and it was less than or equal to the perimeter of the triangle, then the point would be in the triangle. I didn't have a chance to test it yet, though. Yay, finals are over for me! (I only had one "real" final. )
Offline
Actually, here's another, simpler solution:
Given triangle ABC, and point D, find the sum of the areas of ABD, ACD, and BCD (you can use Heron's Formula). If the sum of the areas is equal to the area of ABC, the point is within the triangle. If the sum of the areas is greater than that of ABC, the point is outside the triangle.
See diagram in my other post.
EDIT: Wait, I just remembered reading something...
So, the area of a triangle can be computed by its three vertices.
Last edited by amcerbu (2012-05-22 23:40:49)
Offline
Oh duh, why didn't I think of that? I mean, I know absolutley nothing about Hero/Heron's formula. I have not made a project about it at all, of course not. I most definitely haven't memorized Area=sqrt(s(s-a)(s-b)(s-c) ), where a, b, and c are the lenths of the three sides of the triangle, and s is the semi-perimeter of the triangle ( (a+b+c)/2). Well, now that we know how to find if a point is in a triangle, we can make an equasion to find the plane created by three points, and then make an equasion to find where a 3-dimentional line intersects it so we can see if the line goes through the triangle. If it does, we can make an equasion to find the correct way the line should bounce off the triangle. Good luck on your chemistry final, amcerbu! And good luck to you, Hardmath123, on any finals you may have.
Last edited by applejack (2012-05-23 01:34:55)
Offline
Phew, now school is over for me. I'm leaving for a 16-day trip on monday, so we don't have much time before I am only able to contribute very infrequently. I'm sure summer will be all to short, but I want to finish my app during it.
Offline
Gosh, I'm tired. I just wrote an extremely long email to my friend. I seem to write so much nowadays, maybe because I have so much to say, and because I can type so much more quickly now. I feel like resting, and can't wait until you guys end school and have more time to scratch.
Offline
Yeah, but I think C and C++ work in the compiler too.
amcerbu wrote:
Oh, I had something different in mind with the tiles. Let me get back to you on that one, I've gotta do something else right now. You're doing it in Obj-C, right?
Offline
Yeah. If you could describe exactly what you're trying to do, I'll see if I can put together some C-syntax code for you. Out of curiosity, what compiler and/or IDE are you using? If you're writing for iPhone, I assume you're using a Mac, which probably means... XCode?
Offline
Yes, XCode! You can only use XCode on Macs, so I use my sister's. The confusing thing is that I don't really know what I am doing. I know what I want the final output to be, but object-oriented languages are still pretty confusing to me. I'm not sure if this is possible, but I want a bunch of UIViews in a NSMutuableArray. I would like the views to be different places on the screen, in rows and columbs. The game will be kind of like Music Marathon, but the little rectangles will be 60x60 pixels, and move freely around the screen (although I could have fixed UIViews, and have the subviews that are UIImageViews inside of them move around, instead). I want all the views to have subviews of type UIImageView. I want each subview to have my tileset picture. That way, I don't have to have individual pictures of each of my tiles (so far, I have 74). I can just move around my tileset image in each one so that the tile I want is in the center of each one.
If what I have stated above is impossible, then try to help me achieve my goal:
I want to have many tiles on the screen. I want the tiles to be drawn from a array/string/matrix of some sort (you could use NSArray and NSString, I think. NSMutuableArray makes it possible to edit the array). I don't want the tiles to be fixed in one place. I want the game to move around the player more than the player moves across the game, if you get what I mean. Feel free to ask clarifying questions. Thanks^1000!
Offline
Yeah, I'm looking at it... I'm not that familiar with all those data types in the Objective-C class library. I'm gonna try to figure this out with a little help from my friend Google. I'll get back to you later today or sometime tomorrow.
Offline
applejack wrote:
Yes, XCode! You can only use XCode on Macs, so I use my sister's. The confusing thing is that I don't really know what I am doing. I know what I want the final output to be, but object-oriented languages are still pretty confusing to me. I'm not sure if this is possible, but I want a bunch of UIViews in a NSMutuableArray. I would like the views to be different places on the screen, in rows and columbs. The game will be kind of like Music Marathon, but the little rectangles will be 60x60 pixels, and move freely around the screen (although I could have fixed UIViews, and have the subviews that are UIImageViews inside of them move around, instead). I want all the views to have subviews of type UIImageView. I want each subview to have my tileset picture. That way, I don't have to have individual pictures of each of my tiles (so far, I have 74). I can just move around my tileset image in each one so that the tile I want is in the center of each one.
If what I have stated above is impossible, then try to help me achieve my goal:
I want to have many tiles on the screen. I want the tiles to be drawn from a array/string/matrix of some sort (you could use NSArray and NSString, I think. NSMutuableArray makes it possible to edit the array). I don't want the tiles to be fixed in one place. I want the game to move around the player more than the player moves across the game, if you get what I mean. Feel free to ask clarifying questions. Thanks^1000!
Hey Applejack, I'm pretty fluent in Obj-C, so I can help you out there! You can make it with only one UIView, and store the values in an NSMutableArray. Then you can draw the UIView by invoking the drawRect: command and iterating through the array. If you need more help. let me know.
EDIT: Rereading your post, it looks like you're n=unfamiliar with Quartz 2D—that's the Mac/iOS drawing library native to Mac. Of course, the industry standard is OpenGL.
Last edited by Hardmath123 (2012-05-28 06:04:37)
Offline
I know about drawRect:, but I want to know if I can center the image in the view without having it overflow through the edges, so that I can use just my tileset image, and not a bunch of little tiles. I won't have time to test it out though, unfortunatly, becace I'm leaving for a 16/7 weeks. It will be even longer than Hardmath123's two week long vacation not so long ago! D: I hope you all have a nice time, and I will probably not be able to visit this collab very much, or maybe even not at all. Thank you guys very much, and see you in 16 days!
Hardmath123 wrote:
Hey Applejack, I'm pretty fluent in Obj-C, so I can help you out there! You can make it with only one UIView, and store the values in an NSMutableArray. Then you can draw the UIView by invoking the drawRect: command and iterating through the array. If you need more help. let me know.
EDIT: Rereading your post, it looks like you're n=unfamiliar with Quartz 2D—that's the Mac/iOS drawing library native to Mac. Of course, the industry standard is OpenGL.
Offline
applejack wrote:
I know about drawRect:, but I want to know if I can center the image in the view without having it overflow through the edges, so that I can use just my tileset image, and not a bunch of little tiles. I won't have time to test it out though, unfortunatly, becace I'm leaving for a 16/7 weeks. It will be even longer than Hardmath123's two week long vacation not so long ago! D: I hope you all have a nice time, and I will probably not be able to visit this collab very much, or maybe even not at all. Thank you guys very much, and see you in 16 days!
Hardmath123 wrote:
Hey Applejack, I'm pretty fluent in Obj-C, so I can help you out there! You can make it with only one UIView, and store the values in an NSMutableArray. Then you can draw the UIView by invoking the drawRect: command and iterating through the array. If you need more help. let me know.
EDIT: Rereading your post, it looks like you're n=unfamiliar with Quartz 2D—that's the Mac/iOS drawing library native to Mac. Of course, the industry standard is OpenGL.
You can draw images on an NSView and scale/modify/position them with the NSImage library and transformations. Anyway, drop me a comment when you get back and I'll explain it further. Enjoy your trip!
Offline