|
Kind of an odd spam post though?
|
|
|
|
|
|
How can you insert the elastic walls of the snooker table and make all the 16 balls move and bounce off the walls?
Can someone pliz help me with the code of a functional snooker game using C programing.
|
|
|
|
|
Sorry this site does not provide code to order. To creatre such a program would need a good knowledge of C programming, general GUI processes and (probably) Windows GDI+ or Direct2D. This is not something that you could create in an afternoon.
|
|
|
|
|
|
Francis Lagar wrote: How can you insert th
Steps
1. Learn basic C
2. Learn how to get and use some additional basic libraries for C.
3. Learn basic UI interactions.
4. Draw some basic shapes using 1-3 on your monitor which are sort of like what you want. It will improve later.
5. Learn collision mathematics and how it relates to games. This is not going to be simple. You could skip this and just guess unless you really want it to look right.
6. Create actual program by breaking into parts. For smooth movement you are going to need how to make the graphics move incrementally. This can either be simple (but jerky) or you can spend a lot of time learning it.
I suspect there is going to be more to the above.
|
|
|
|
|
Depending on the environment, basic real-time programming and multithreading may also be required.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
It returns an int . That int is similar to what you would get from open(2) , and can be used in many places any other file descriptor could be - e.g read(2) , write(2) , and, of course, close(2) . I think that some fcntl(2) operations are also valid for socket fds. But you'll probably get a error if you try to use lseek(2) on a socket. If you prefer, its a file descriptor that describes a socket. Just like open("/dev/ttyS0", O_RDWR) returns a file descriptor that describes a serial port and open("data.txt", O_WRONY|O_CREAT, 0660) returns a file descriptor that describes (presumbably) a text file in the CWD, or open("/tmp/mypipe", O_RDONLY) returns a file descriptor that might be a named pipe.
NB: in the notation open(2) the (2) refers to the manual section that describes the system. So for example man 1 stat produces the man page for the stat command, whereas man 2 stat produces the man page for the stat system call.
Keep Calm and Carry On
|
|
|
|
|
Member 14968771 wrote: RTFM is not an answer I am looking for...
Except that it is the answer: socket(2) - Linux manual page[^], explains it in detail.
|
|
|
|
|
Member 14968771 wrote: If it returns "error" - how can I actually read it / get it in human / verbose format ?
Just noting that in my experience what the number means, even if you can map it, is very seldom useful.
In general it can only mean about two things.
1. You passed in an incorrect parameter. But it will tell you nothing about what or why it is wrong.
2. The system is way overloaded. It too will tell you nothing about what is specifically overloaded.
And just to be clear the error here has nothing to do with actually communicating.
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
Unless you're hacking on glibc, you really shouldn't be digging around in the system include files.
Nevertheless, doing some googling around I learned this:
k5054@localhost]$ cpp -include stdlib.h -dM /dev/null | grep -E 'define (THROW|LEAF)>'
#define __LEAF , __leaf
#define THROW __attribute ((nothrow __LEAF))
[k5054@localhost]$
That means that the __THROW notation adds the attributes nothrow and leaf to the definition of the function. You can read up on them here:Function Attributes - Using the GNU Compiler Collection (GCC)
Surely by now you know how to handle libc errors:
#include <cerrno>
#include <cstring>
int ret = socket(foo, bar, baz);
if(ret == -1 ) {
switch (errno) {
case EACCES:
break;
default:
std::cerr << "socket() returned error " << strerror(errno) << '\n';
}
}
Note: It is perfectly possible for errno to be set by a system call, even when the system call is successful. That can happen when the system call itself makes another system call, which sets errno. I ran into this not so long ago with code like:
errno = 0;
ret = somefn(blah, blah, blah);
if (errno) {
} In this case somefn() called otherfn() to do its work. otherfn() failed, setting errno in the process, and then somfn handled the error, and perhaps called anotherfn() which succeded, but did not set errno back to zero. Moral of the story: you need to check if your system call returned an error (normally -1, but not neccesarily), and then check the value of errno.
Keep Calm and Carry On
|
|
|
|
|
Hello,
I am working on a robots projects and part of this project is a local map space (2D array/matrix).
This map consists of 201 x 201 1cm squares, with the robot sitting in the middle.
From a robots point of view this local map has a index range of -100x -100y to +100x +100y
With the robot sitting at cell 0,0, obviously from a c/c++ point of view.
The array/matrix is addressed from 0x to 200x and 0y to 200y. (0x,0y being array/matrix bottom left)
I have successfully implement a robot rolling road and robot array/matrix rotations.
However, for some reason my brain has gone into gimble lock.
Trying to figure how to find the array/matrix x,y indices for a number of cells at a given angle from the middle of the map (robots position).
The code below produces perfect result for 100 cells at 0,45,90,135,180,225,270,315 degrees but at other angle is fails, can someone help please.
Many thanks in advance imk
#define LOCAL_MAP_NUM_X_CELLS 201
#define LOCAL_MAP_NUM_Y_CELLS 201
#define DEG_TO_RAD 0.0174533
struct LocalMap
{
unsigned char Cell[ LOCAL_MAP_NUM_Y_CELLS ][ LOCAL_MAP_NUM_X_CELLS ];
}; typedef struct LocalMap LocalMap;
int Cell_X;
int Cell_Y;
ComputeLocalMapCellIntexFromCentre( 315 , 100 , &Cell_X , &Cell_Y );
int ComputeLocalMapCellIntexFromCentre( double Angle , int NumberCells , int *pCell_X , int *pCell_Y )
{
double AngleAdjustMent;
double Sin = sin( Angle * DEG_TO_RAD);
double Cos = cos( Angle * DEG_TO_RAD);
double NumCells;
AngleAdjustMent = ( fabs( sin( Angle * DEG_TO_RAD)) + fabs( cos( Angle * DEG_TO_RAD )) );
NumCells = (double)NumberCells;
NumCells = round( (double)NumCells * AngleAdjustMent );
NumberCells = (int)NumCells;
*pCell_X = (int)round( Sin * NumberCells ) + ( LOCAL_MAP_NUM_X_CELLS - 1 ) / 2;
*pCell_Y = (int)round( Cos * NumberCells ) + ( LOCAL_MAP_NUM_Y_CELLS - 1 ) / 2;
return( TRUE );
}
modified 26-Mar-23 13:43pm.
|
|
|
|
|
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:
void find_endpoint (double angle, int* x, int* y)
{
assert (angle >= 0 && angle < 360);
if (angle >= 315 || angle <= 45) {
*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) {
*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) {
*x = 0;
*y = (int)round((1 + sin (angle * DEG_TO_RAD)) * LOCAL_MAP_NUM_Y_CELLS / 2);
}
else {
*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:
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:
int main (int argc, char **argv)
{
int x1, y1;
std::vector<pt> cells;
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.
|
|
|
|
|
Normalize the angle to <= 90 (right angle triangle); calculate the opposite and adjacent sides; offset origin x by (int) adjacent, and origin y by (int) opposite.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Hello this is Gulshan Negi
Well, I want to know about how C and C++ differ in terms of their memory management and performance capabilities, and what strategies can be used to optimize code and improve performance in each language?
I need some suggestions on this.
Thanks
|
|
|
|
|
The difference will depend very much on the implementation of the compiler and associated libraries.
|
|
|
|
|
Gulshan Negi wrote: performance in each language
Performance is based on the following
# Requirements - Most significant.
# Architecture/Design (high level)
# Implementation
# Technology - Least significant.
Your question fits into the last one of those.
If you are very, very good at the first three or your problem (the complete solution) is very, very small then the last one might be the most significant.
|
|
|
|
|
Write a C program to Count the Lines, Words and Characters in a given text
*Write a C program to count the lines, words and characters in a given text.
*At the time of execution, the program should print the following message on the console as:
*Enter lines of text (enter END to complete)
For example, if the user gives the input as:
Enter lines of text (enter END to complete)
CodeTantra developed a Robotic tool
In the year 2016
END
then the program should print the result as:
Character = 51, Words = 9, Lines = 2
Note: Do use the printf() function with a newline character (\n) at the end
|
|
|
|
|
This is your assignment, so you are expected to do the work. If you do not understand the assignment then you need to ask your teacher for guidance.
|
|
|
|
|
Read a line, fgets[^] is your friend.
If the line read is exactly END than your task is complete: report the line, word and character count.
On the other hand, if the line read is not the termination one, then increment the line count, and get the first character (incrementing the character count): if it is a blank then set a flag was_blank = 1; and go on. If it is not a blank then increment the word count and clear the flag: was_blank = 0; .
Go on with the next character (if any): increment the character count and if the character is
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
There is no simple answer to such a question as the lifetime of a variable will depend on its usage and requirements. You need to analyse your code to decide what is best in each specific case.
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|