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

C / C++ / MFC

 
GeneralRe: How to implement "\n" in QT QDebug class Pin
Richard MacCutchan22-Apr-23 21:10
mveRichard MacCutchan22-Apr-23 21:10 
Questionget text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Ilqar Sadiqov8-Apr-23 9:02
Ilqar Sadiqov8-Apr-23 9:02 
AnswerRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Richard MacCutchan8-Apr-23 21:01
mveRichard MacCutchan8-Apr-23 21:01 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Ilqar Sadiqov9-Apr-23 20:46
Ilqar Sadiqov9-Apr-23 20:46 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Richard MacCutchan9-Apr-23 22:14
mveRichard MacCutchan9-Apr-23 22:14 
GeneralRe: get text of SysListView32(LVS_OWNERDATA+LVS_OWNERDRAWFIXED) items from other applications Pin
Dave Kreskowiak10-Apr-23 1:59
mveDave Kreskowiak10-Apr-23 1:59 
Questionc++ Pin
EvelynChristian7-Apr-23 5:01
EvelynChristian7-Apr-23 5:01 
AnswerRe: c++ Pin
Victor Nijegorodov7-Apr-23 6:45
Victor Nijegorodov7-Apr-23 6:45 
GeneralRe: c++ Pin
Richard MacCutchan7-Apr-23 7:20
mveRichard MacCutchan7-Apr-23 7:20 
GeneralRe: c++ Pin
Victor Nijegorodov7-Apr-23 7:30
Victor Nijegorodov7-Apr-23 7:30 
GeneralRe: c++ Pin
jschell7-Apr-23 9:13
jschell7-Apr-23 9:13 
GeneralRe: c++ Pin
Richard MacCutchan7-Apr-23 21:57
mveRichard MacCutchan7-Apr-23 21:57 
QuestionSnooker Game programing using c Programing. Pin
Francis Lagar3-Apr-23 7:37
Francis Lagar3-Apr-23 7:37 
AnswerRe: Snooker Game programing using c Programing. Pin
Richard MacCutchan3-Apr-23 9:57
mveRichard MacCutchan3-Apr-23 9:57 
AnswerRe: Snooker Game programing using c Programing. Pin
Gerry Schmitz3-Apr-23 10:19
mveGerry Schmitz3-Apr-23 10:19 
AnswerRe: Snooker Game programing using c Programing. Pin
jschell4-Apr-23 6:24
jschell4-Apr-23 6:24 
GeneralRe: Snooker Game programing using c Programing. Pin
Daniel Pfeffer4-Apr-23 7:40
professionalDaniel Pfeffer4-Apr-23 7:40 
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.

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.