This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#276 2012-05-21 12:01:26

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: applejack-amcerbu-Hardmath123-collab

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?


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#277 2012-05-21 19:24:51

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

Hmm... lemme think.

EDIT: I could just check the internet for an equasion, but that would take all the fun away.

Last edited by applejack (2012-05-21 19:25:28)


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#278 2012-05-21 23:02:22

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

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

 

#279 2012-05-22 00:24:38

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

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. 

http://i45.tinypic.com/26242zn.png

Last edited by amcerbu (2012-05-22 16:37:07)

Offline

 

#280 2012-05-22 19:15:55

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#281 2012-05-22 19:57:27

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

You're probably right.  I'll have to check it out later.  I've got a chemistry final tomorrow.

Offline

 

#282 2012-05-22 21:43:24

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

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

http://i49.tinypic.com/nn0028.png

So, the area of a triangle can be computed by its three vertices.

Last edited by amcerbu (2012-05-22 23:40:49)

Offline

 

#283 2012-05-23 01:34:17

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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)


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#284 2012-05-24 00:49:55

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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.


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#285 2012-05-24 01:13:41

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

Nice!  I've got tomorrow off (it's the day I would have a history exam, but I've already taken the AP Euro exam), then Algebra II on Friday, and then Summer!

Offline

 

#286 2012-05-24 14:11:05

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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.


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#287 2012-05-25 10:22:29

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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?


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#288 2012-05-25 14:58:55

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

I'm done with school!  I've got about a week to do stuff before I go on a trip, so I can work a lot on programming/Scratch.

Offline

 

#289 2012-05-25 15:12:01

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

Oh yay!

amcerbu wrote:

I'm done with school!  I've got about a week to do stuff before I go on a trip, so I can work a lot on programming/Scratch.


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#290 2012-05-25 15:53:36

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

So... Could you help me?


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#291 2012-05-25 19:14:07

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

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

 

#292 2012-05-25 22:28:34

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

Yes, XCode!  big_smile  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!


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#293 2012-05-26 12:49:02

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

Invisible bump.


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#294 2012-05-26 13:15:17

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

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

 

#295 2012-05-26 13:20:52

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

smile

Last edited by applejack (2012-05-26 13:22:49)


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#296 2012-05-26 15:19:27

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

This may help you help me.

Last edited by applejack (2012-05-27 23:01:30)


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#297 2012-05-28 06:02:03

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: applejack-amcerbu-Hardmath123-collab

applejack wrote:

Yes, XCode!  big_smile  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.  smile

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)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#298 2012-05-28 09:03:11

applejack
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: applejack-amcerbu-Hardmath123-collab

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

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.


http://i.imgur.com/zKzps.png
http://blocks.scratchr.org/API.php?action=onlineStatus&type=square&user=applejack -I'm http://blocks.scratchr.org/API.php?action=onlineStatus&type=text&user=applejack

Offline

 

#299 2012-05-28 12:18:05

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: applejack-amcerbu-Hardmath123-collab

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

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!  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#300 2012-05-28 13:02:43

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: applejack-amcerbu-Hardmath123-collab

Yeah, sorry about that, applejack.  I don't know a thing about the Obj-C class library.

Offline

 

Board footer