Computernerd
Computernerd

Reputation: 7772

Print heart shape with words inside

I am trying to draw a heart shape with words inside as a surprise for a friend tomorrow but i cant figure out how to put the words inside the heart . I am only able to draw the heart shape

Code to draw Heart

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double x,y;
    double size=10;



    for (x=0;x<size;x++)
    {
        for (y=0;y<=4*size;y++)
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );

            if (dist1 < size + 0.5 || dist2 < size + 0.5 )
            cout<<"V";
            else
            cout<<" ";


        }
        cout<<endl;

    }

    for ( x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++)
        cout<<" ";

        for (y=0; y<4*size + 1 - 2*x; y++)
        cout<<"V";

        cout<<endl;
    }

    system("PAUSE");
}

I need help putting words inside the heart shape

Upvotes: 2

Views: 47245

Answers (3)

Xar Hang
Xar Hang

Reputation: 21

Try this code:

#include <iostream>
#include <cmath>    
using namespace std;

int main() {
    cout<<"Print Heart....C++\n";

        int n=7; //size of heart
        for(int i=-3*n/2;i<=n;i++){
            for(int j=-3*n/2;j<=3*n/2;j++){
    /* inside either diamond or two circles */
            if((abs(i)+abs(j)<n)||((-n/2-i)*(-n/2-i)+(n/2-j)*(n/2-j)<=n*n/2)||((-n/2-i)*(-n/2-i)+(-n/2-j)*(-n/2-j)<=n*n/2)){
                   cout<<"v ";
               }
               else{
                   cout<<"  ";
               }
            }
               cout<<"\n";
        }
        cout<<"\n\n\nPlease don\'t forget to like.... :) :) :) \n";
        cout<<"Credit: Heart Of Java : https://code.sololearn.com/caiu85u6tr30/#java";
    return 0;
}

Demo here: https://code.sololearn.com/cuXe0axsK8R2/#cpp

Upvotes: 0

Tom Fenech
Tom Fenech

Reputation: 74685

Pretty much the same as the other answer but I had already started so I thought I may as well finish. As a bonus you can specify what line of the "V" shape it prints on.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double x, y, size=10;
    string message(" hello there ");
    int print_line = 4;
    if (message.length() % 2 != 0) message += " ";

    for (x=0;x<size;x++) 
    {
        for (y=0;y<=4*size;y++)   
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );

            if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
                cout << "V";
            }
            else cout << " ";
        }
        cout<<"\n";
    }

    for (x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++) cout << " ";

        for (y=0; y<4*size + 1 - 2*x; y++) 
        {            
            if (x >= print_line - 1 && x <= print_line + 1) {
                int idx = y - (4*size - 2*x - message.length()) / 2;
                if (idx < message.length() && idx >= 0) {
                    if (x == print_line) cout<<message[idx];
                    else cout << " ";
                }
                else cout << "V";
            }
            else cout << "V";
        }
        cout<<endl;
    }
}

Upvotes: 2

Taylor Brandstetter
Taylor Brandstetter

Reputation: 3623

You could just wait until you get to a pre-specified position in the heart and print out a message instead of the "V"s, like this:

    char message[] = " MY MESSAGE ";
    for ( x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++)
        cout<<" ";

        for (y=0; y<4*size + 1 - 2*x; y++)
        {
            if (x == 1 && y == (2*size - strlen(message)/2))
            {
                cout << message;
                y += strlen(message)-1;
            }
            else
                cout<<"V";
        }

        cout<<endl;
    }

The y += strlen(message)-1; is to advance the column index according to the number of characters printed. (2*size - strlen(message)/2) is a position which will center the string.

If you want to obfuscate the code as much as possible (so you don't know what the message is until the code runs), you could use a hash table to map positions to letters or something like that.

Upvotes: 1

Related Questions