tags:

views:

104

answers:

1

Possible Duplicate:
loop with if consition

   int i, j, c = 0;
    int num = ring.points_.size();

    for (i = 0, j = num-1; i < num; j = i++) {
        if ((((ring.points_[i].y_ <= pt.y_) && (pt.y_ < ring.points_[j].y_)) ||
            ((ring.points_[j].y_ <= pt.y_) && (pt.y_ < ring.points_[i].y_))) &&
            (pt.x_ < (ring.points_[j].x_ - ring.points_[i].x_) * (pt.y_ - ring.points_[i].y_) / (ring.points_[j].y_ - ring.points_[i].y_) + ring.points_[i].x_))
            c = !c;
    }
    return c; 

this is function of "In Side" with parameter as pt(point) and ring(from line-string(list of points))... but here i am not getting if condition inside the loop.

A: 

I have just added the missing definitions, declaration and initialization, not touching your code, and it seems to get inside the if statement. Put break point at c = !c; line and run.

#include <vector>
using namespace std;

struct _point
{
    int x_;
    int y_;
};

struct _ring
{
    vector<_point> points_;
};

int func(const _ring & ring, const _point & pt)
{
    int i, j, c = 0;
    int num = ring.points_.size();

    for (i = 0, j = num-1; i < num; j = i++)
    {
        if ((((ring.points_[i].y_ <= pt.y_) && (pt.y_ < ring.points_[j].y_)) ||
            ((ring.points_[j].y_ <= pt.y_) && (pt.y_ < ring.points_[i].y_))) &&
            (pt.x_ < (ring.points_[j].x_ - ring.points_[i].x_) * (pt.y_ - ring.points_[i].y_) / (ring.points_[j].y_ - ring.points_[i].y_) + ring.points_[i].x_))
            c = !c;
    }

    return c; 
}

int main()
{
    // fill in this ring.point_ list
    _ring ring;
    _point temp;

    // 1st point
    temp.x_ = 2;
    temp.y_ = 3;
    ring.points_.push_back(temp);

    // 2nd point
    temp.x_ = 7;
    temp.y_ = 5;
    ring.points_.push_back(temp);

    // 3rd point
    temp.x_ = 0;
    temp.y_ = -12;
    ring.points_.push_back(temp);

    // 4th point
    temp.x_ = -8;
    temp.y_ = -1;
    ring.points_.push_back(temp);

    // point to check ?
    _point pt;
    pt.x_ = 0;
    pt.y_ = 0;

    // call the function
    func(ring, pt);
}
Vertilka
hmm yes thanks but i ma not getting loops logic..still i am trying to understand it.
piyapiya
i am not getting working of j in loop.
piyapiya
ohh i see i think loop is working clock wise.
piyapiya
i am not getting (pt.x_ < (ring.Points_[j].x_ - ring.Points_[i].x_) * (pt.y_ - ring.Points_[i].y_) / (ring.Points_[j].y_ - ring.Points_[i].y_) + ring.Points_[i].x_)) c = !c; this..
piyapiya
can you explain me (pt.x_ < (ring.Points_[j].x_ - ring.Points_[i].x_) * (pt.y_ - ring.Points_[i].y_) / (ring.Points_[j].y_ - ring.Points_[i].y_) + ring.Points_[i].x_)) c = !c; this.. i am not getting this one
piyapiya