size_t StatusStructure::getClosestTo(const Point& point, float& outDist) const {
    size_t index = 0;
    auto currentFrag = getAt(index);

    if (currentFrag == nullptr) {
        return index;
    }

    float dist = getPositionOnScanLine(currentFrag, point);
    size_t closestIndex = index;
    outDist = dist;

    for (;;) {
        if (dist > 0 && leftChild(index) != nullptr) {
            index = getLeftChildIndex(index);
        } else if (dist < 0 && rightChild(index) != nullptr) {
            index = getRightChildIndex(index);
        } else {
            break;
        }

        currentFrag = getAt(index);
        dist = getPositionOnScanLine(currentFrag, point);

        if (std::fabs(outDist) > std::fabs(dist)) {
            closestIndex = index;
            outDist = dist;
        }
    }

    return closestIndex;
}
Example #2
0
long MainPlayer::edgeStabilityHeuristic(const GameBoard &gameBoard, Tile tile) const {
    auto gameSize = gameBoard.getGameSize();
    std::vector<Direction> directions{{0,  1},
                                      {0,  -1},
                                      {1,  0},
                                      {-1, 0}};

    std::vector<Cell> corners{{0,            0},
                              {0,            gameSize - 1},
                              {gameSize - 1, 0},
                              {gameSize - 1, gameSize - 1}};
    auto board = gameBoard;
//    board.print(std::cerr);
    long stableCellsCount = 0;
    for (Cell corner : corners) {
        if (board.getAt(corner) != tile) {
            continue;
        }
        ++stableCellsCount;
        for (Direction direction : directions) {
            auto current = corner;
            current.move(direction);
            while (board.isCorrect(current) && board.getAt(current) == tile) {
                ++stableCellsCount;
                board.setAt(current, EMPTY);
//                board.print(std::cerr);
                current.move(direction);
            }
        }
    }
    return stableCellsCount;
}
Example #3
0
bool
CoordinateSequence::hasRepeatedPoints() const
{
    const std::size_t size=getSize();
	for(std::size_t i=1; i<size; i++) {
		if (getAt(i-1)==getAt(i)) {
			return true;
		}
	}
	return false;
}
Example #4
0
const Coordinate*
CoordinateSequence::minCoordinate() const
{
	const Coordinate* minCoord=NULL;
	const std::size_t size=getSize();
	for(std::size_t i=0; i<size; i++) {
		if(minCoord==NULL || minCoord->compareTo(getAt(i))>0) {
			minCoord=&getAt(i);
		}
	}
	return minCoord;
}
Example #5
0
void Sphere::createIndices() {
	// create index elements for quads.
	int dx = 0;
	for(int i = 0; i < slices; ++i) {
		for(int j = 0; j < stacks; ++j) {
			index_elements[dx++] = getAt(i,j);
			index_elements[dx++] = getAt(i+1,j);
			index_elements[dx++] = getAt(i+1,j+1);
			index_elements[dx++] = getAt(i,j+1);
		}
	}
}
Example #6
0
int main()
{
    List ls;
    ls.count = 0;
    ls.first = NULL;

    int number = 0;
    scanf("%d", &number);
    int i = 0, temp;
    while (i < number) {
        scanf("%d", &temp);
        insertAt(&ls, i, temp);
        i++;
    }
    print(&ls, stdout);
    printf("\n");

    i = 1;
    while (i < 6) {
        insertFront(&ls, i);
        i++;
    }
    print(&ls, stdout);
    printf("\n");

    i = 1;
    int nums = getSize(&ls);
    while (i < nums) {
        deleteAt(&ls, i);
        i++;
        nums--;
    }
    print(&ls, stdout);
    printf("\n");

    i = 0;
    nums = getSize(&ls);
    while (i < getSize(&ls)) {
        if (getAt(&ls, i) < 3) {
            deleteAt(&ls, i);
            i--;
        } else {
            setAt(&ls, i, getAt(&ls, i) * 10);           
        }
        i++;
    }
    print(&ls, stdout);


    return 0;
}
LineSegmentPtr StatusStructure::fragmentLeftOf(const Point& point) const {
    float dist;
    size_t closestIndex = getClosestTo(point, dist);
    auto frag = getAt(closestIndex);

    if (frag == nullptr) {
        return nullptr;
    }

    if (dist > 0 || nearZero(dist)) {
        // The fragment at closestIndex is not left of the point (equal or right)
        // find the first left neighbour that is left of the point.
        while (dist > 0 || nearZero(dist)) {
            size_t leftNeighbourIndex = getLeftNeighbourIndex(closestIndex);

            if (leftNeighbourIndex == closestIndex) {
                // no left neighbour...
                return nullptr;
            }

            closestIndex = leftNeighbourIndex;

            frag = getAt(closestIndex);

            if (frag == nullptr) {
                return nullptr;
            }

            dist = getPositionOnScanLine(frag, point);
        }
    } else {
        // There could be other fragments that intersect the scan line at the same position as frag (e.g same startpoint as frag or intersection with frag)
        // Move right until we find a fragment that is right (or equal) to the point.. return the last fragment that was left
        for (;;) {
            size_t rightNeighbourIndex = getRightNeighbourIndex(closestIndex);
            auto rightFrag = getAt(rightNeighbourIndex);
            dist = getPositionOnScanLine(rightFrag, point);

            if (dist > 0 || nearZero(dist) || rightFrag == nullptr || rightNeighbourIndex == closestIndex) {
                break;
            }

            closestIndex = rightNeighbourIndex;
            frag = rightFrag;
        }
    }

    return frag;
}
Example #8
0
static int isRotateMirror()
{
	char piece;
	int i, j, i2, j2;

	i2 = getRowSize();
	for (i = 1; i <= ((getRowSize() + 1) / 2); i++) {
		j2 = getColSize();
		for (j = 1; j <= getColSize(); j++) {
			piece = getAt(i, j);
			switch (getAt(i2, j2)) {
				case NW:
					if (piece != SE)
						return false;
			                break;
				case NE:
					if (piece != SW)
						return false;
			                break;
				case SW:
					if (piece != NE)
						return false;
			                break;
				case SE:
					if (piece != NW)
						return false;
			                break;
				case NS:
					if (piece != NS)
						return false;
			                break;
				case WE:
					if (piece != WE)
						return false;
			                break;
				case EMPTY:
					if (piece != EMPTY)
						return false;
			                break;
				default:
					break;
			}
			j2--;
		}
		i2--;
	}
	return true;
}
void StatusStructure::remove(const LineSegmentPtr& fragment) {
    bool valid;
    size_t index = indexOf(fragment, valid);

    if (!valid) {
        return;
    }

    m_index_map.erase(fragment);

    if (leftChild(index) == nullptr) {
        move(getRightChildIndex(index), index);
        return;
    } else if (rightChild(index) == nullptr) {
        move(getLeftChildIndex(index), index);
        return;
    }

    // get direct left neighbour of index (most right child in left subtree)
    size_t directLeftNeighbourIndex = getLeftChildIndex(index);
    while (rightChild(directLeftNeighbourIndex) != nullptr) {
        directLeftNeighbourIndex = getRightChildIndex(directLeftNeighbourIndex);
    }

    insertAt(index, getAt(directLeftNeighbourIndex));
    move(getLeftChildIndex(directLeftNeighbourIndex), directLeftNeighbourIndex);
}
void StatusStructure::move(size_t from, size_t to) {
    if (to >= m_tree.size()) {
        return;
    }

    // Movement must be recursive and breath-first instead of depth-first (otherwise we would overwrite elements before they are moved)
    std::queue<std::pair<size_t, size_t>> moveQueue;
    moveQueue.push(std::make_pair(from, to));

    while (!moveQueue.empty()) {
        from = moveQueue.front().first;
        to = moveQueue.front().second;
        moveQueue.pop();

        auto toMoveFrag = getAt(from);
        insertAt(to, toMoveFrag);

        if (toMoveFrag != nullptr) {
            // move the children of the moved element
            insertAt(from, nullptr);                                                           // clear the element
            moveQueue.push(std::make_pair(getLeftChildIndex(from), getLeftChildIndex(to)));    // move left children
            moveQueue.push(std::make_pair(getRightChildIndex(from), getRightChildIndex(to)));  // move right children
        }
    }
}
Example #11
0
void
CoordinateSequence::expandEnvelope(Envelope &env) const
{
	const std::size_t size = getSize();
	for (std::size_t i=0; i<size; i++)
        env.expandToInclude(getAt(i));
}
Example #12
0
String& String::insert(size_t pos, char c, size_t rep)
{
    KEEPOLD;

    //
    // preconditions
    //

    if(pos > length())
        strError("String::insert", "OutOfRange");
    //
    // operations
    //

    if(rep == 1)
        doReplace(pos, 0, &c, 1);
    else if(rep > 0)
        insert(pos, String(c, rep));

    //
    // post conditions
    //

    assert(length() == (rep + OLD.length()));
#ifndef NDEBUG
    for(int i = 0; i < rep; i++)
        assert(getAt(i + pos) == c);
#endif /* NDEBUG */

    return *this;
}
Example #13
0
String& String::append(char c, size_t rep)
{
    KEEPOLD;

    //
    // preconditions
    //

    if(rep == NPOS)
        strError("String::append", "OutOfRange");

    //
    // operations
    //

    if(rep == 1)
        doReplace(length(), 0, &c, 1);
    else if(rep > 0)
        operator+=(String(c, rep));

    //
    // post conditions
    //

    assert((OLD.length() + rep) == length());
#ifndef NDEBUG
    for(int i = 0; i < rep; i++) {
        int j = i + OLD.length();
        assert(getAt(j) == c);
    }
#endif /* NDEBUG */

    return *this;
}
Example #14
0
String::String(char  c, size_t rep)
{
    //
    // preconditions
    //

    if(rep == NPOS)
        strError("String::String", "OutOfRange");

    //
    // operations
    //

    if(rep == 0) {
        srep = 0;
    }
    else {
        srep = StringRep::getNew(rep);
        memset(srep->str, c, rep);
    }

    //
    // post conditions
    //

    assert(rep == 0 || srep != 0);
    assert(length() == rep);
#ifndef NDEBUG
    for(int i = 0; i < rep; i++)
        assert(getAt(i) == c);
#endif /* NDEBUG */
}
Example #15
0
//Unit testing
//main()
int main(int argc, char** argv)
{
    char* fileData = readFileIntoRam(argv[1]);

    //1st pass with a small bug with the last string
    //fix: fixed the bug with the missing 0 terminator
    int i = split(fileData, ' ');
    //test the size
    printf("Size is: [%d]\n", i);

    //test the get
    int j;
//    for(j=0; j < getSize(); j++)
//    {
//        printf("[%d]=[%s]\n", j, getAt(j));
//    }

    //reverse
    for(j=getSize()-1; j>=0; j--)
    {
        printf("[%d]=[%s]\n", j, getAt(j));
    }

    return 0;
}
LineSegmentPtr StatusStructure::fragmentRightOf(const Point& point) const {
    float dist;
    size_t closestIndex = getClosestTo(point, dist);
    auto frag = getAt(closestIndex);

    if (frag == nullptr) {
        return nullptr;
    }

    if (dist < 0 || nearZero(dist)) {
        // find the first right neighbour that is right of the point
        while (dist < 0 || nearZero(dist)) {
            size_t rightNeighbourIndex = getRightNeighbourIndex(closestIndex);

            if (rightNeighbourIndex == closestIndex) {
                return nullptr;
            }

            closestIndex = rightNeighbourIndex;

            frag = getAt(closestIndex);

            if (frag == nullptr) {
                return nullptr;
            }

            dist = getPositionOnScanLine(frag, point);
        }
    } else if (nearZero(dist)) {
        // find the last left neighbour that is still right of the point
        for (;;) {
            size_t leftNeighbourIndex = getLeftNeighbourIndex(closestIndex);
            auto leftFrag = getAt(leftNeighbourIndex);
            dist = getPositionOnScanLine(leftFrag, point);

            if (dist < 0 || nearZero(dist) || leftFrag == nullptr || leftNeighbourIndex == closestIndex) {
                break;
            }

            closestIndex = leftNeighbourIndex;
            frag = leftFrag;
        }
    }

    return frag;
}
Example #17
0
PathPoint Path::update(Vector3<float> refPos, Vector3<float> playerPos)
{
  float D_val;
  PathPoint current = getCurrent();
  PathPoint previous = getPrevious();
  float diffX = refPos.x - current.getPosition().x;
  float diffY = refPos.y - current.getPosition().y;
  float diffZ = refPos.z - current.getPosition().z;
  float playerDistFromPlane = 0;
  float firstDistFromPlane = 0;
  Vector3<float> vect1 (current.getPosition().x - previous.getPosition().x,
			current.getPosition().y - previous.getPosition().y,
			current.getPosition().z - previous.getPosition().z);

  Vector3<float> vect2 (current.getPosition().x + current.getUp().x,
			current.getPosition().y + current.getUp().y,
			current.getPosition().z + current.getUp().z);

  Vector3<float> normal;
  float distance = sqrt(diffX * diffX + diffY * diffY + diffZ * diffZ);
  PathPoint firstChoice = getAt(current.getFirstID());

  if (distance < RANGE_CHECK) {
    if (current.getNumberOfIDs() == 1) {

      setChoice(current.getFirstID());
      return getCurrent();
    }

    normal = vect1.Cross(vect2).Normalized();
    D_val = (current.getPosition().x * normal.x + current.getPosition().y 
	     * normal.y + current.getPosition().z * normal.z) * -1.0f;
    
    playerDistFromPlane = (normal.x * playerPos.x) + 
       (normal.y * playerPos.y) + (normal.z * playerPos.z) + D_val;
    
    if (abs(playerDistFromPlane) < MID_BUFFER_WIDTH && 
	current.getNumberOfIDs() == 3) {
      setChoice(current.getThirdID());
      return getCurrent();
    }
    
    firstDistFromPlane = normal.x * firstChoice.getPosition().x 
       + normal.y * firstChoice.getPosition().y 
       + normal.z * firstChoice.getPosition().z + D_val;

    if (playerDistFromPlane / abs(playerDistFromPlane) == 
	firstDistFromPlane / abs(firstDistFromPlane)) {
      setChoice(current.getFirstID());
      return getCurrent();
    }
    else {
      setChoice(current.getSecondID());
      return getCurrent();
    }
  }
  return getCurrent();
}
Example #18
0
BOOL TNode::isExpanded(ULONG nItem, BOOL bItemexpanded /*= TRUE*/)
{
	TNode* pNode = getAt(nItem, bItemexpanded);
	if (pNode != NULL)
	{
		return pNode->isExpanded();
	}
	return FALSE;
}
Example #19
0
int main(int argc, char **argv)
{
	int *pointer;
	int pointerWert =255;
	pointer=&pointerWert;
	getAt(pointerWert, 1);
	return 0;
	
}
Example #20
0
// generate spherical coordinates.
void Sphere::createVertices() {
	for(float stack = 0; stack <= stacks; ++stack) {
		float theta = stack/(stacks) * PI;
		float sin_theta = sinf(theta);
		float cos_theta = cosf(theta);
			
		for(float slice = 0; slice <= slices; ++slice) {
			float phi = slice/(slices) * TWO_PI;
			float sin_phi = sinf(phi);
			float cos_phi = cosf(phi);

			float x = sin_theta * cos_phi;
			float y = sin_theta * sin_phi;
			float z = cos_theta;
			
			float u = (sin_theta * cos_phi * 0.5) + 0.5;
			float v = (sin_theta * sin_phi * 0.5) + 0.5;
			int index = (int)(slice + (stack * (slices+1)));
			vertices[index].pos.set(x * radius*1.05,y * radius, z * radius);
			vertices[index].norm.set(x,y,z);
			vertices[index].col.set(x,y,z);
			vertices[index].tex.set(u,v);
		}
	}
	
	// now all vertices have been created we can add the tangent and binormal.
	for(int i = 0; i <= stacks; ++i) {
		for(int j = 0; j <= slices; ++j) {
			// last one.
			Vec3 va = vertices[getAt(i,j)].pos;
			Vec3 vb;
			if(j == slices) {
				vb = vertices[getAt(i-1, j)].pos;
			}
			else {
				vb = vertices[getAt(i + 1, j)].pos;		
			}
			// TODO add data structure with tangent
			//Vec3 dir = vb - va;
			//dir.normalize();
			//vertices[getAt(i,j)].tangent = dir;
		}
	}
};
Example #21
0
/**
 * Retrieve a ptr to an element at the given position in a linked list.
 * \note An O(n) opperation.
 *
 * @param llist     Ptr to the list to retrieve the element from.
 * @param position  Position of the element to be retrieved.
 * @return          Ptr to the element if found at the given position.
 */
void *List_GetAt(const LinkList *llist, listindex_t position)
{
    if(llist)
    {
        LinkList *list = (LinkList*) llist;
        return getAt(list, position);
    }

    return NULL;
}
Example #22
0
/**
 * Retrieve a ptr to the element at the back of a linked list.
 *
 * @param llist     Ptr to the list to retrieve the element from.
 * @return          @c true, iff an element was found at back of the list.
 */
void *List_GetBack(const LinkList *llist)
{
    if(llist)
    {
        LinkList *list = (LinkList*) llist;
        return getAt(list, list->state->numElements - 1);
    }

    return NULL;
}
Example #23
0
/**
 * Retrieve a ptr to the element at the front of a linked list.
 *
 * @param llist     Ptr to the list to retrieve the element from.
 * @return          @c true, iff an element was found at front of the list.
 */
void *List_GetFront(const LinkList *llist)
{
    if(llist)
    {
        LinkList *list = (LinkList*) llist;
        return getAt(list, 0);
    }

    return NULL;
}
int CNSegmentArray::getSegmentCount(int iType)
{
  int iCount = 0;
  for (int iIndex = 0; (iIndex < getCount()); iIndex++) {
    CNSegment* p = getAt(iIndex);
    if (p->getSegmentType() == iType) {
      iCount++;
    }
  }
  return iCount;
}
bool CNProbeSetArray::isSketchSubset()
{
  unsigned int uiCount = 0;
  for (int iIndex = 0; (iIndex < getCount()); iIndex++) {
    CNProbeSet* p = getAt(iIndex);
    if (p->isUseForSketch()) {
      uiCount++;
    }
  }
  return (uiCount == getCount());
}
int CNProbeSetArray::getProcessCount()
{
  unsigned int uiCount = 0;
  for (int iIndex = 0; (iIndex < getCount()); iIndex++) {
    CNProbeSet* p = getAt(iIndex);
    if (p->isProcess() ) {
      uiCount++;
    }
  }
  return uiCount;
}
void StatusStructure::insert(LineSegmentPtr fragment, const Point& scanLinePosition) {
    size_t currentIndex = 0;
    auto currentFrag = getAt(currentIndex);

    while (currentFrag != nullptr) {
        if (currentFrag == fragment) {
            return;
        }

        if (isALeftOfBOnScanLine(*fragment, *currentFrag, scanLinePosition)) {
            currentIndex = getLeftChildIndex(currentIndex);
        } else {
            currentIndex = getRightChildIndex(currentIndex);
        }

        currentFrag = getAt(currentIndex);
    }

    insertAt(currentIndex, fragment);
}
/*public*/
void
CoordinateArraySequence::add(size_t i, const Coordinate& coord,
                                       bool allowRepeated)
{
    // don't add duplicate coordinates
    if (! allowRepeated) {
      size_t sz = size();
      if (sz > 0) {
        if (i > 0) {
          const Coordinate& prev = getAt(i - 1);
          if (prev.equals2D(coord)) return;
        }
        if (i < sz) {
          const Coordinate& next = getAt(i);
          if (next.equals2D(coord)) return;
        }
      }
    }

    vect->insert(vect->begin()+i, coord);
}
Example #29
0
/*public*/
void
CoordinateSequence::add(const Coordinate& c, bool allowRepeated)
{
	if (!allowRepeated) {
        std::size_t npts=getSize();
		if (npts>=1) {
			const Coordinate& last=getAt(npts-1);
			if (last.equals2D(c))
                return;
		}
	}
	add(c);
}
Example #30
0
void
Liztiter_old_ATTLC::reset(unsigned i)
{
	if(!i) reset0();
	else {
	    if(i > theLizt->length()) i = theLizt->length();
	    index = i;
	    if(i == 0 || i == theLizt->length()) {
	        pred = theLizt->t;
	    }
	    else pred = getAt(i - 1);
	}
}