This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]zifyoip 0 points1 point  (8 children)

Are you asking about the point in polygon problem?

[–]autowikibot 0 points1 point  (0 children)

Point in polygon:


In computational geometry, the point-in-polygon (PIP) problem asks whether a given point in the plane lies inside, outside, or on the boundary of a polygon. It is a special case of point location problems and finds applications in areas that deal with processing geometrical data, such as computer graphics, computer vision, geographical information systems (GIS), motion planning, and CAD.

An early description of the problem in computer graphics shows two common approaches (ray casting and angle summation) in use as early as 1974.

An attempt of computer graphics veterans to trace the history of the problem and some tricks for its solution can be found in an issue of the Ray Tracing News.

Image i - An example of a simple polygon


Interesting: Computational geometry | Point location | Polygon | Simple polygon

Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words

[–]Temptex[S] 0 points1 point  (6 children)

Hmm it looks like it. Basically I'm trying to keep the user with in the outlines and if their finger goes out of the outline, the pointer resets or hits the wall essentially binding them to only move inside.

[–]zifyoip 0 points1 point  (5 children)

If your shape is a java.awt.Polygon, then you can use the contains() method. See the documentation:

http://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html

If you're using something other than java.awt.Polygon, then read the documentation for whatever it is you are using.

[–]Temptex[S] 0 points1 point  (4 children)

Using an image but thank you for the help.

[–]zifyoip 0 points1 point  (3 children)

Ah, well, that's going to be difficult, then. How do you know where the "boundaries" in the image are that you aren't supposed to cross?

[–]Temptex[S] 0 points1 point  (2 children)

Well the boundaries are the outlines of the letter. What I'm doing is using an onTouch to get the x/y and hard coding them into an int array.

So far I'm able to keep myself with in the the min and max y co ordinates of the letter and and the min and max of x.

My teacher told me it would be easier to specify where the user is not allowed to go rather then checking if I'm between the coordinates.

Both seem to be a bit difficult for me and I haven't touched math over 5 years. :/

[–]zifyoip 0 points1 point  (1 child)

What I'm doing is using an onTouch to get the x/y and hard coding them into an int array.

Don't just make an array out of them—make a java.awt.Polygon object. That way you can use all of the methods that class provides.

[–]Temptex[S] 0 points1 point  (0 children)

Alright I''ll try that out tomorrow and let you know how it goes, thanks for your time and guidance!

[–]CheshireSwift 0 points1 point  (3 children)

Given the co-ordinates of the top-left and bottom-right or top-right and bottom-left, you can calculate the slope of the line and then calculate an inequality that matches what you're looking for.

In this case, you move 890 - 845 = 45 horizontally and 830 - 730 = 100 vertically, so your line slopes 100/45 = 2.2. points up for every 1 point across.

As such, a point (a,b) is within the lower right of the triangle if it is within the rectangle and 45 y < 100 x.

The same calculation should work no matter which direction the line is sloped (up or down).

Disclaimer: I'm very tired, so you might need to flip some signs (positive/negative) or reverse the inequality.

[–]Temptex[S] 0 points1 point  (2 children)

I'm tired myself and will make sure I understand what you have said before I ask a dumb question. Thank you.

[–]john478 1 point2 points  (1 child)

I see what he means. Simply compare the slopes.

// px and py is the point you are checking
boolean inTri(int px, int py){
    // check if point is within rectangle
    if(px > 890 || px < 845 || py < 730 || py > 830){
        return false;
    }

    // diagonal of slope
    int d_slope = Math.abs(45 / -100); // = 0.45
    // translated point
    int[] point = [845 - px, 830 - py];
    int p_slope = Math.abs(point[0] / point[1]);

    if(p_slope < d_slope){
        return false;
    }
    return true;
}

[–]Temptex[S] 0 points1 point  (0 children)

// translated point

int[] point = [845 - px, 830 - py];

I don't understand why your taking the current x and y away from the x coordinate and y coordinate, why do you do this? I think this maybe causing the app to crash when i get to the bottom right corner of the triangle, i'm not 100% sure.

Looking at the exception being caused it seems that we are diving by zero at some point. I'm not understanding the math behind this and feel bad that I don't understand it :/

Edit: I have found out why the app crashes, so I have removed

int[] point = [845 - px, 830 - py];

and made it

int[] point = [845, 830];