Points on circle

This is a discussion on Points on circle within the C++ Programming forums, part of the General Programming Boards category; I loaded an image into a 2D array, where each index is a pixel. The image is just a white ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Points on circle

    I loaded an image into a 2D array, where each index is a pixel. The image is just a white circle on black background.

    I am having trouble locating the upper-most, left-most, right-most and bottom-most pixels of that circle.

    Code:
    // 255 = white, 0 = black
    //left
    if (circle[i][j] == 255 && circle[i-1][j] == 0 && circle[i][j-1] == 0)
      cout << "left: " << i << "," << j << endl;
    
    //right
    if (circle[i][j] == 255 && circle[i-1][j] == 0 && circle[i][j+1] == 0)
      cout << "right: " << i << "," << j << endl;
    
    //up
    if (circle[i][j] == 255 && circle[i-1][j] == 0 && circle[i][j-1] == 0)
      cout << "up: " << i << "," << j << endl;
    
    //down
    if (circle[i][j] == 255 && circle[i+1][j] == 0 && circle[i][j-1] == 0)
      cout << "down: " << i << "," << j << endl;
    This is not working out for me, what would be the proper way to do this ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The first question to ask yourself is "why does my 'left' check check up? and why does my 'right' check check left and down? and why does my 'up' check check left? and why does my down check check right and up?"

    Once you've dealt with that, then you can ask yourself "what are i and j? And what happens when i is 0, so that i can't even do i-1?"

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's a bit tricky to figure out what the leftmost or whatever pixel of an image is all at once like that. It might be easier if you recorded the leftmost pixel found so far, and update that when you find an even further left pixel. Something like this:
    Code:
    int leftmost_x = INT_MAX, leftmost_y = -1;
    for(int i = 0; i < WIDTH; i ++) {
        for(int j = 0; j < HEIGHT; j ++) {
            if(circle[i][j] == 255 && i < leftmost_x) {
                leftmost_x = i;
                leftmost_y = j;
            }
        }
    }
    
    if(leftmost_y >= 0) {
        // leftmost pixel found at (leftmost_x, leftmost_y)
    }
    else {
        // no pixels found at all
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  3. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  4. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 03:30 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21