how to create a triangle in c++ without using opengl, directx, or other api
I want to know if it po开发者_运维问答ssible for me to create a line or a triangle or any shape with only c++ code.
You can create one very easily - to display it you will need to talk to the operating system some how.
edit: of course it depends how good a gui you want
std::cout << " * "<<std::endl;
std::cout << " *** "<<std::endl;
std::cout << " *****"<<std::endl;
Good enough?
You can't do this in plain C++, since the standard does not define any kind of GUI which would allow you to.
To do that you will need either a platform-dependent API or library or some library that abstracts the differences between a certain set of platforms (e.g., Qt, wxWidgets).
int
main()
{ }
Closest I can come up with. Can't think of a valid one character sequence for the top though.
It seems you don't want character graphics.
Then, if you want to restrict yourself to pure standard C++, no external libraries (not even OS API), then you're limited to creating graphics files.
The easiest is to use some text-based graphics format, such as SVG.
Then you can do almost anything, as text, using say just cout <<
.
But in any case, you need a viewer (AFAIK all web browsers except IE support SVG), and a spreadsheet is one kind of viewer. So if you just want to graph a function, then generate a comma-delimited text file of values (CSV, Comma-Separated Values), and view it via graphics presentation in a spreadsheet program.
EDIT: Oh, I forgot to mention old-time method of sending graphics commands to the printer. Things to look into for that include printer's documentation, and PostScript. As with SVG and as with CSV it only involves generating textual output from C++.
Cheers & hth.,
Based on the OP comments elsewhere in this thread, it sounds like he wants to do this by direct manipulation of the low-level instructions on the various pieces of hardware used for rendering images. To do this:
- Understand the hardware you have for performing this. This means doing exensive research into how the hardware is designed and used.
- Understand how to directly invoke the manufacturer's device drivers. Again, this requires extensive research and is usually made more complicated by the fact that hardware manufacturers generally obfuscate their device driver source code, if they publish it at all, and they usually don't release documentation for the device driver code.
- Understand how you can directly manipulate hardware using C++ to write your own API to manage the hardware functions.
Once that's done, you can use your API to draw a triangle or any other polygon you like. Personally, this seems a bit too much for me, and so I'll use an existing API. But this might be a worthwhile learning experience for somebody without a spouse and children.
Opengl, directx are api's that make calling the systems hardware easier. So anything that you can do in either directx or opengl you can do in c++ code, its just much much harder.
Yes:
cout << "|";
this prints a line.
Ignoring that your question is ambiguous, no.
You will need some way of getting access to the pixel data. This is difficult to do on modern systems as the graphical hardware is so varied and the drivers are complex and make use of undocumented GPU assembly instructions.
You could in theory use the code in the open source ATI/Catalyst or nouveau (NVidia) drivers for linux but ultimately, you need something (GDI, DirectX, OpenGL, Cairo, SDL or others) to get a display surface you can write pixel data to.
Once you have the pixel surface, you can create the triangle, line or other shape using math, e.g.:
#define RED 1
#define GREEN 2
#define BLUE 3
char screen[1024][768][3];
Then for a line from (10,10) to (110,10):
for (int x = 10; x < 110; ++x)
screen[10][x][RED] = 0xFF;
or a triangle:
for (int y = 10; y < 110; ++y)
for (int x = 10; x <= y; ++x)
screen[y][x][RED] = 0xFF;
This is what the cairo software renderer is doing, for example, but optimises different cases.
Since no facilities to working with graphics defined in C++ standard library, you are left with your OS API and 3-rd party libraries.
I'm a big fan of SDL. It's a simple API for creating a window and receiving input events from the keyboard and mouse. You can easily draw a shape on a black window; check out this example.
精彩评论