Click here to Skip to main content
15,881,881 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
SuggestionMessage Closed Pin
29-Mar-23 15:32
Member 1496877129-Mar-23 15:32 
GeneralRe: Socket or file descriptor ? Pin
k505429-Mar-23 17:18
mvek505429-Mar-23 17:18 
GeneralRe: Socket or file descriptor ? Pin
Richard MacCutchan29-Mar-23 21:38
mveRichard MacCutchan29-Mar-23 21:38 
GeneralRe: Socket or file descriptor ? Pin
jschell30-Mar-23 5:45
jschell30-Mar-23 5:45 
GeneralMessage Closed Pin
30-Mar-23 6:28
Member 1496877130-Mar-23 6:28 
GeneralRe: Socket or file descriptor - more questions ? Pin
k505430-Mar-23 9:31
mvek505430-Mar-23 9:31 
QuestionFind X,Y index into 2D array/matrix using an angle and number of cells Pin
imk12326-Mar-23 7:37
imk12326-Mar-23 7:37 
AnswerRe: Find X,Y index into 2D array/matrix using an angle and number of cells Pin
Mircea Neacsu26-Mar-23 8:04
Mircea Neacsu26-Mar-23 8:04 
On the run now so I don't have time for a detailed answer but Bresenham's line algorithm[^] is your friend.

Edit: Here is a detailed solution.
First you have to determine the end point of your line. There might be smarter ways, but a direct way to do it is this:
C++
/* Find endpoint of line with given angle*/
void find_endpoint (double angle, int* x, int* y)
{
  assert (angle >= 0 && angle < 360);
  if (angle >= 315 || angle <= 45) //end point on right side
  {
    *x = LOCAL_MAP_NUM_X_CELLS;
    *y = (int)round((1 + sin (angle * DEG_TO_RAD)) * LOCAL_MAP_NUM_Y_CELLS / 2);
  }
  else if (angle <= 135) //endpoint on top side 
  {
    *y = LOCAL_MAP_NUM_Y_CELLS;
    *x = (int)round((1 + cos (angle * DEG_TO_RAD)) * LOCAL_MAP_NUM_X_CELLS / 2);
  }
  else if (angle < 225) //endpoint of left side
  {
    *x = 0;
    *y = (int)round((1 + sin (angle * DEG_TO_RAD)) * LOCAL_MAP_NUM_Y_CELLS / 2);
  }
  else //endpoint of bottom side
  {
    *y = 0;
    *x = (int)round((1 + cos (angle * DEG_TO_RAD)) * LOCAL_MAP_NUM_X_CELLS / 2);
  }
}
Once you have the starting point (100, 100) and the endpoint of your line, it is simply a question of applying the Bresensham algorithm to determine the points on the line:
C++
// a point in matrix
struct pt {
  int x;
  int y;
};

void bresenham (int x0, int y0, int x1, int y1, std::vector<pt>& cell)
{
  int dx = abs (x1 - x0);
  int sx = x0 < x1 ? 1 : -1;
  int dy = -abs (y1 - y0);
  int sy = y0 < y1 ? 1 : -1;
  int error = dx + dy;

  while (1)
  {
    cell.push_back ({ x0, y0 });
    if (x0 == x1 && y0 == y1)
      break;
    int e2 = 2 * error;
    if (e2 >= dy)
    {
      if (x0 == x1)
        break;
      error = error + dy;
      x0 = x0 + sx;
    }
    if (e2 <= dx)
    {
      if (y0 == y1)
        break;
      error = error + dx;
      y0 = y0 + sy;
    }
  }
}
Note that this is a direct transcription of the algorithm on the Wikipedia page mentioned above. The number of cells obviously varies so the simplest for me was to keep it in a vector. If you need to have strictly C solution, you will have to manage that yourself.

The caller program looks like this:
C++
int main (int argc, char **argv)
{
  int x1, y1;
  std::vector<pt> cells;

  //First quadrant, angle < 45
  find_endpoint (30., &x1, &y1);
  bresenham (100, 100, x1, y1, cells);
  cout << "Angle 30 degrees. End point: "<<x1 << ',' << y1 << endl;
  print_cells (cells);
  cout << endl;
//...

Mircea


modified 26-Mar-23 19:48pm.

AnswerRe: Find X,Y index into 2D array/matrix using an angle and number of cells Pin
Gerry Schmitz26-Mar-23 8:50
mveGerry Schmitz26-Mar-23 8:50 
QuestionWhat is the difference in C and C++ in terms of memory management ? Pin
Gulshan Negi20-Mar-23 22:57
professionalGulshan Negi20-Mar-23 22:57 
GeneralRe: What is the difference in C and C++ in terms of memory management ? Pin
Richard MacCutchan21-Mar-23 2:11
mveRichard MacCutchan21-Mar-23 2:11 
AnswerRe: What is the difference in C and C++ in terms of memory management ? Pin
jschell21-Mar-23 5:41
jschell21-Mar-23 5:41 
QuestionC language Pin
Pavani M18-Mar-23 5:24
Pavani M18-Mar-23 5:24 
AnswerRe: C language Pin
Richard MacCutchan18-Mar-23 6:53
mveRichard MacCutchan18-Mar-23 6:53 
AnswerRe: C language Pin
CPallini19-Mar-23 22:33
mveCPallini19-Mar-23 22:33 
QuestionMessage Closed Pin
5-Mar-23 5:33
Member 149687715-Mar-23 5:33 
AnswerRe: Reorganizing code Pin
Richard MacCutchan5-Mar-23 21:56
mveRichard MacCutchan5-Mar-23 21:56 
QuestionMessage Closed Pin
3-Mar-23 5:42
Member 149687713-Mar-23 5:42 
AnswerRe: How to implement variable amount of parameters? Pin
Mircea Neacsu3-Mar-23 6:10
Mircea Neacsu3-Mar-23 6:10 
AnswerRe: How to implement variable amount of parameters? Pin
k50543-Mar-23 6:18
mvek50543-Mar-23 6:18 
AnswerRe: How to implement variable amount of parameters? Pin
Richard MacCutchan3-Mar-23 21:57
mveRichard MacCutchan3-Mar-23 21:57 
AnswerRe: How to implement variable amount of parameters? Pin
jschell6-Mar-23 6:20
jschell6-Mar-23 6:20 
GeneralRe: How to implement variable amount of parameters? Pin
charlieg19-Mar-23 11:47
charlieg19-Mar-23 11:47 
QuestionC++ non-null pointers Pin
Mircea Neacsu2-Mar-23 7:47
Mircea Neacsu2-Mar-23 7:47 
AnswerRe: C++ non-null pointers Pin
jschell6-Mar-23 6:22
jschell6-Mar-23 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.