C/C++ Graphicl Heart
This code will make a graphical Heart in C programming which will move around your screen and will change it's colors, It's a Great Program Try Now!
code:
#include <graphics.h>
#include <math.h>
#include <conio.h> // For kbhit
#include <dos.h> // For delay
double hearta(double k) {
return 15 * pow(sin(k), 3);
}
double heartb(double k) {
// Invert the y-coordinates to flip the heart
return -(12 * cos(k) - 5 * cos(2 * k) - 2 * cos(3 * k) - cos(4 * k));
}
void drawHeart(int xOffset, int yOffset, int color) {
setcolor(color);
for (int i = 0; i < 1000; i++) {
double x = hearta(i / 100.0) * 20 + xOffset;
double y = heartb(i / 100.0) * 20 + yOffset;
putpixel(x, y, color);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int xOffset = 200, yOffset = 200;
int color = RED;
int direction = 1;
while (!kbhit()) { // Check for key press
cleardevice();
drawHeart(xOffset, yOffset, color);
xOffset += direction;
if (xOffset > getmaxx() - 100 || xOffset < 100) {
direction = -direction;
color = (color % 15) + 1; // Change color
}
delay(50); // Delay for 50 milliseconds
}
closegraph();
return 0;
}