-2

You may have noticed my last query on a similar task. I am trying to replicate this structure:

********************
*********  *********
********    ********
*******      *******
******        ******
*****          *****
****            ****
***              ***
**                **
*                  *
**                **
***              ***
****            ****
*****          *****
******        ****** 
*******      *******
********    ********
*********  *********
********************

If you think of it as composed of 4 triangles - I am able to produce each of the corners individually, then to put them under one another. I do not know how I would go about putting them together in a larger construct though. Would I attempt this, or would I approach the problem by tackling it as if were a single construct and work on it line by line as I did with the individual triangle parts? I just assumed that the triangles into a bigger thing might be a shortcut.

madth3
  • 7,275
  • 12
  • 50
  • 74
Azz
  • 89
  • 1
  • 3
  • 8
  • What's wrong with calculating the positions of the vertexes, subtending a line for each of the edges, then testing whether each position in your grid (inside the bounding box) is inside or outside the figure? – Marcin Sep 05 '11 at 16:16
  • 2
    Sounds like this might need a "homework" tag. – Carl F. Sep 05 '11 at 16:17
  • oh jeez I'm quite new to python. @Marcin. Could you please elaborate? I do not know what: *subtending means, and what for each of the edges - implies for it *testing whether each position in your grid - I don' know anything about grids, testing or boundaries in python, but I would love to learn - could you please give me a bit of a hint – Azz Sep 05 '11 at 16:18
  • *What bounding boxes are *This is just like homework as @CARL F. suggested but I don't know how to add tags afterwards and is for a very basic level so I think that the solution is much simpler than Marcin has suggested. this is specifically just a book exercise since I'm picking up python out of interest but I don't know what the conventions are here so should I have homework tagged it? So far I only use 'python'. thanks. – Azz Sep 05 '11 at 16:21
  • @Rooney: nothing I am suggesting is specific to Python, or even really computers. I suggest you read some basic computer graphics or geometry texts. – Marcin Sep 05 '11 at 17:33

5 Answers5

2

This worked for me

w = 20
lines = []
for y in xrange(w / 2, 0, -1):
    lines.append("".join(("*" * y, " " * (w - (y * 2)), "*" * y))
lines += reversed(lines[:-1])
for l in lines:
    print l

You could shrink it down more if you wanted to as well:

lines = ["".join(("*" * y, " " * (w - (y * 2)), "*" * y)) for y in xrange(w / 2, 0, -1)]
print "\n".join(lines + lines[-2::-1])

Output w = 20:

********************
*********  *********
********    ********
*******      *******
******        ******
*****          *****
****            ****
***              ***
**                **
*                  *
**                **
***              ***
****            ****
*****          *****
******        ******
*******      *******
********    ********
*********  *********
********************
GWW
  • 43,129
  • 11
  • 115
  • 108
1

I would treat it as a different problem. If you imagine an origin at the centre of the figure, then you only want to print a * where the "Manhattan distance" to that cell is greater than half the length of the edge. For example, you could do the following:

import sys

r = 5

for y in range(-r,r+1):
    for x in range(-r,r+1):
        c = ' ' if (abs(x) + abs(y)) < r else '*'
        sys.stdout.write(c)
    print

... which produces this:

***********
***** *****
****   ****
***     ***
**       **
*         *
**       **
***     ***
****   ****
***** *****
***********
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
1
print '\n'.join('*' * (20 - i) + ' ' * (i * 2) + '*' * (20 - i) for i in range(0, 20))
print '\n'.join('*' * i + ' ' * (40 - i * 2) + '*' * i for i in range(0, 20))

http://codepad.org/Z46ldEOG

develerx
  • 600
  • 5
  • 13
0

If your instructor was looking for simple loops, this may help (even though its probably too late, and it only really works with even "w" value's)

w = 20  
for a in range (0,w/2):
    print((w/2-a)*"*"+2*a*" "+(w/2-a)*"*")
for a in range (2,w/2+1):
    print(a*"*"+(w-2*a)*" "+a*"*")

Or if you wanted to take out the variable "w" all together:

for a in range (0,10):
    print((10-a)*"*"+2*a*" "+(10-a)*"*")
for a in range (2,11):
    print(a*"*"+(20-2*a)*" "+a*"*")
Button 783
  • 21
  • 1
0

How about:

W = 10

for i in range(10):
    this_str = "*"*(W-i)+" "*i
    print this_str+this_str[::-1]

for i in range(9,-1,-1):
    this_str = "*"*(W-i)+" "*i
    print this_str+this_str[::-1]

Peer pressure at work. Here's a shorter version to keep up with all my compatriots:

W=10
lines = ["*"*(W-i)+" "*2*i+"*"*(W-i) for i in range(W)]
print "\n".join(lines + lines[-2::-1])
Carl F.
  • 6,718
  • 3
  • 28
  • 41