15,940,550 members
Home / Discussions / C / C++ / MFC

# C / C++ / MFC

 Message Closed 29-Mar-23 15:32 Member 14968771 29-Mar-23 15:32
 Re: Socket or file descriptor ? k505429-Mar-23 17:18 k5054 29-Mar-23 17:18
 Re: Socket or file descriptor ? Richard MacCutchan29-Mar-23 21:38 Richard MacCutchan 29-Mar-23 21:38
 Re: Socket or file descriptor ? jschell30-Mar-23 5:45 jschell 30-Mar-23 5:45
 Message Closed 30-Mar-23 6:28 Member 14968771 30-Mar-23 6:28
 Re: Socket or file descriptor - more questions ? k505430-Mar-23 9:31 k5054 30-Mar-23 9:31
 Find X,Y index into 2D array/matrix using an angle and number of cells imk12326-Mar-23 7:37 imk123 26-Mar-23 7:37
 Re: Find X,Y index into 2D array/matrix using an angle and number of cells Mircea Neacsu26-Mar-23 8:04 Mircea Neacsu 26-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& 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 cells; //First quadrant, angle < 45 find_endpoint (30., &x1, &y1); bresenham (100, 100, x1, y1, cells); cout << "Angle 30 degrees. End point: "<
 Re: Find X,Y index into 2D array/matrix using an angle and number of cells Gerry Schmitz26-Mar-23 8:50 Gerry Schmitz 26-Mar-23 8:50
 What is the difference in C and C++ in terms of memory management ? Gulshan Negi20-Mar-23 22:57 Gulshan Negi 20-Mar-23 22:57
 Re: What is the difference in C and C++ in terms of memory management ? Richard MacCutchan21-Mar-23 2:11 Richard MacCutchan 21-Mar-23 2:11
 Re: What is the difference in C and C++ in terms of memory management ? jschell21-Mar-23 5:41 jschell 21-Mar-23 5:41
 C language Pavani M18-Mar-23 5:24 Pavani M 18-Mar-23 5:24
 Re: C language Richard MacCutchan18-Mar-23 6:53 Richard MacCutchan 18-Mar-23 6:53
 Re: C language CPallini19-Mar-23 22:33 CPallini 19-Mar-23 22:33
 Message Closed 5-Mar-23 5:33 Member 14968771 5-Mar-23 5:33
 Re: Reorganizing code Richard MacCutchan5-Mar-23 21:56 Richard MacCutchan 5-Mar-23 21:56
 Message Closed 3-Mar-23 5:42 Member 14968771 3-Mar-23 5:42
 Re: How to implement variable amount of parameters? Mircea Neacsu3-Mar-23 6:10 Mircea Neacsu 3-Mar-23 6:10
 Re: How to implement variable amount of parameters? k50543-Mar-23 6:18 k5054 3-Mar-23 6:18
 Re: How to implement variable amount of parameters? Richard MacCutchan3-Mar-23 21:57 Richard MacCutchan 3-Mar-23 21:57
 Re: How to implement variable amount of parameters? jschell6-Mar-23 6:20 jschell 6-Mar-23 6:20
 Re: How to implement variable amount of parameters? charlieg19-Mar-23 11:47 charlieg 19-Mar-23 11:47
 C++ non-null pointers Mircea Neacsu2-Mar-23 7:47 Mircea Neacsu 2-Mar-23 7:47
 Re: C++ non-null pointers jschell6-Mar-23 6:22 jschell 6-Mar-23 6:22
 Last Visit: 31-Dec-99 18:00     Last Update: 18-Jul-24 4:49 Refresh ᐊ Prev1...45464748495051525354 Next ᐅ

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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