37
\$\begingroup\$

Draw the parabolic trajectory of a thrown ball.

The input is the ball's initial upward velocity, a positive integer v. Every second, the ball moves 1 space right and v spaces vertically, and then v decreases by 1 to due to gravity. So, the upward velocity eventually steps down from v to 0 and down to -v, finally falling back down to its initial height.

The ball's positions trace a parabola. At horizontal position x, its height is y=x*(2*v+1-x)/2, with (0,0) the ball's initial position at the bottom left.

Output ASCII art of the ball's trajectory with O's on the coordinates it ever occupies. The output should be a single multi-line piece of text, not an animation of the path over time.

The output should have no leading newlines and at most one trailing newline. The bottom line should be flush with the left edge of the screen, i.e. have no extra leading spaces. Trailing spaces are OK. You may assume the output line width fits in the output terminal.

v=1

 OO 
O  O

v=2

  OO  
 O  O 

O    O

v=3

   OO   
  O  O  

 O    O 


O      O

v=4

    OO    
   O  O   

  O    O  


 O      O 



O        O

v=10

          OO          
         O  O         

        O    O        


       O      O       



      O        O      




     O          O     





    O            O    






   O              O   







  O                O  








 O                  O 









O                    O

Related: Bouncing ball simulation


Leaderboard:

var QUESTION_ID=111861,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/111861/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
6
  • \$\begingroup\$ Can we ouput a list of lines? \$\endgroup\$
    – Riker
    Commented Mar 2, 2017 at 3:26
  • \$\begingroup\$ @Riker Nope, string with newlines. \$\endgroup\$
    – xnor
    Commented Mar 2, 2017 at 3:27
  • \$\begingroup\$ loosely related: codegolf.stackexchange.com/q/110410 \$\endgroup\$
    – Titus
    Commented Mar 2, 2017 at 14:06
  • \$\begingroup\$ Do I only need to account for V > 0? \$\endgroup\$
    – nmjcman101
    Commented Mar 2, 2017 at 17:56
  • \$\begingroup\$ Yes, v will be positive. \$\endgroup\$
    – xnor
    Commented Mar 2, 2017 at 18:02

35 Answers 35

1
2
0
\$\begingroup\$

AHK, 93 bytes

m=0
n=1
f=%1%-1
Loop,%1%{
r=%r%{VK20 %f%}O{VK20 %m%}O{`n %n%}
m+=2
n++
f--
}
FileAppend,%r%,*

If I could figure out how to do math inside of repeating keystrokes, that'd be great.
- VK20 equates to a space
- FileAppend outputs to stdout if the filename is *

\$\endgroup\$
0
\$\begingroup\$

Haskell, 77 bytes

n!x=[1..n]>>x
-1#i=[]
x#i=(x-1)#(i+1)++x!"\n"++i!" "++'O':(2*x)!" "++"O"
(#0)

Try it online! Usage: (#0) 5

\$\endgroup\$
0
\$\begingroup\$

Python 2, 59 bytes

f=lambda n,r='O':(r*n and f(n-1,' '+r))+'\n'*n+r+'  '*n+'O'

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 26 bytes

VyhQ=+Y+*Zd\O=+ZQ=tQ)j_.tY

Try it Online

Explanation

VyhQ=+Y+*Zd\O=+ZQ=tQ)j_.tY
                            Implicit Q=input() (speed), Z=0 (position), Y=[].
VyhQ                )       For N in [0,...,2Q+2]...
       +*Zd\O               ... prepend Z spaces to 'O'...
    =+Y                     ... add that line to Y...
             =+ZQ=tQ        ... then update position and speed.
                     j_.tY  Combine the lines to form the parabola.
\$\endgroup\$
0
+100
\$\begingroup\$

APL (Dyalog Unicode), 27 25 bytes

(⌽,⊢)⍉↑↑∘'O'¨1,1-1↓+\⍳⎕+1

Try it online!

-2 bytes from kritixilithos at the APL Orchard.

-2 bytes from Adám.

Explanation

 (⌽,⊢)⍉↑↑∘'O'¨1,1-1↓+\⍳⎕+1
                      ⍳⎕+1  range 1 - n+1
                    +\      cumulative sum over each element
                  1↓        drop the first value (0)
                1-          subtract 1 from each value
              1,            prepend 1 to it.
             ¨              for each element,
        ↑∘'O'               prepend(element-1) spaces to 'O'.
       ↑                    convert to matrix
      ⍉                     transpose
 (⌽,⊢)                      prepend its reverse (palindromize).
\$\endgroup\$
0
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.