Print a smiley face

  • Requirements



    For the sake of the challenge let's define a unit:




    A unit is defined as any point that compose the grid in which the face is drawn (pixels for screen, characters for ASCII art, etc..).




    The minimal requirements for the face are:




    A smiley face is composed of a shape that resemble a circle (not necessarily perfect) of any radius. There must be at least 1 unit for each of the two eyes and at least 2 units for the mouth. Both eyes and mouth should be within the face shape. The mouth can be oriented however you want (happy, sad, indifferent, etc..).




    Here's an example of the output:



     0 0 0    . . .
    0 . . 0 . o o .
    0 --- 0 . \_/ .
    0 0 0 . . .


    Goals



    Of course the main goal is to come up with the shortest code possible, but art is also important.



    Winner



    The winner is chosen based on the amount of votes from the users, in, at least, the next 5 days.


    Wow, 8 questions on the hot question list. Our site should be getting more traffic.

    Isn't indifferent face strictly easier than happy, and sad sometimes more difficult?

    Not going to make this an answer since it doesn't fit the minimum requirements, but I wanted to chime in... `alt + 1` and `alt + 2` will produce ☺ and ☻ respectively. For the sake of argument I'll consider the `alt` key press a character and call that 2 characters.

    Javascript Bookmarklet: `javascript:b=document.body;b.innerHTML='☹'` (Posted here as this post is now protected)

    Will this thread ever end? :)

  • avall

    avall Correct answer

    7 years ago

    Javascript, 340



    var i=0,b=document.body,d=20,n=b.clientWidth,m=b.clientHeight,f="width=10,height=10,"
    var o=new Function("f","i","t",'open("","",f+"left="+i+",top="+t)')
    for(j=0;j<m/d;++j,i=j*d){
    u=Math.sqrt(i*(m-i))
    t=m/2+u*(j%2-0.5)*2
    o(f,i,t)
    i>m/5&&i<0.8*m&&j%2&&o(f,i,t-m/4);
    ((i<m/3&&i>m/4)||(i<3*m/4&&i>2*m/3))&&o(f,i,m/3)
    }





    Javascript, 283



    optimized version (with some improvements & without unnecessary white spaces)



    var i=0,b=document.body,d=20,n=b.clientWidth,m=b.clientHeight,f="width=10,height=10,"
    function o(i,t){open("","",f+"left="+i+",top="+t)}for(j=0;j<m/d;i=++j*d){u=Math.sqrt(i*(m-i));t=m/2+j%2*2*u-u;o(i,t);i>m/5&&i<0.8*m&&j%2&&o(i,t-m/4);((i<m/3&&i>m/4)||(i<3*m/4&&i>2*m/3))&&o(i,m/3)}


    Well.. maybe it isn't as short as you would want to but it's unconventional for sure. It looks better when your browser is maximized. I really enjoyed your question!
    If you want to increase the details just reduce d variable slightly.



    edit: Unfortunately I can't run it on jsfiddle but you can paste it to Javascript console in aby browser. Oh, and enable popups on the page :).



    edit2:
    You can run it making a new html file and paste the code into it:



    <body>
    <script>
    var i=0,b=document.body,d=20,n=b.clientWidth,m=b.clientHeight,f="width=10,height=10,"
    var o=new Function("f","i","t",'open("","",f+"left="+i+",top="+t)')
    for(j=0;j<m/d;++j,i=j*d){
    u=Math.sqrt(i*(m-i))
    t=m/2+u*(j%2-0.5)*2
    o(f,i,t)
    i>m/5&&i<0.8*m&&j%2&&o(f,i,t-m/4);
    ((i<m/3&&i>m/4)||(i<3*m/4&&i>2*m/3))&&o(f,i,m/3)
    }
    </script>
    </body>


    Then when you run it and nothing happens, just enable the popup windows and reload the page.
    In addition I paste
    screenshot


    I see nothing. Do you have a live demo? (like jsfiddle)

    please see edit

    whoa, that's a lot of popups. Sadly, I can't see the whole thing... Could you upload a screenshot please? (and congrats for the unconventional approach!)

    Really original, I like it.

    In the name of Bwian of Judea, what is this...

    it's more like Santa..

    not working for me

    @the_Seppi : code relies on the popups so disable popup blockers, adBlock & related stuff, then refresh your html file in a browser.

    popups enabled, need them for js debugging, ABP disabled on jsfiddle, still not working (Im using Chromium browser)

    You won't run it on jsfiddle. I tried and I failed. Make a new html file locally and insert the code I pasted in my answer. It should work well.

    Now it's working. Took me about 2m to close all popups

    @the_Seppi, right click, close all windows?

    After spending so much time trying to disable annoying popups, enabling them is hard. I disabled adBLock and allowed all popups in my browser...after half the face appears, it decides to turn the blocker back on!

    Worderful answer!

    Amazing out-of-the-box thought! I would never think to do something like this.

    That's outstandingly original!

    It even looks 3D!

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM