[Question]
  [
You will be given a positive integer `N` as input. Your task is to build a Semi-Zigzag, of `N` sides, each of length `N`. Since it is relatively hard to clearly describe the task, here are some examples:
* `N = 1`:
```
O
```
* `N = 2`:
```
O
 O O
```
* `N = 3`:
```
O       O
 O     O
  O O O
```
* `N = 4`:
```
O           O O O O
 O         O
  O       O
   O O O O
```
* `N = 5`:
```
O               O O O O O
 O             O         O
  O           O           O
   O         O             O
    O O O O O               O
```
* `N = 6`:
```
O                   O O O O O O
 O                 O           O
  O               O             O
   O             O               O
    O           O                 O
     O O O O O O                   O O O O O O
```
* `N = 7`:
```
O                       O O O O O O O                       O
 O                     O             O                     O
  O                   O               O                   O
   O                 O                 O                 O
    O               O                   O               O
     O             O                     O             O
      O O O O O O O                       O O O O O O O
```
* [A larger test case with `N = 9`](https://pastebin.com/BHGM6p5y)
As you can see, a Semi-Zigzag is made of alternating diagonal and horizontal lines, and it always begins with a top-left to bottom right diagonal line. Take note that the characters on the horizontal lines are separated by a space.
# Rules
* You may choose any non-whitespace *character* instead of `O`, it may even be inconsistent.
* You may output / return the result as a String or as a list of Strings, each representing one *line*.
* You may have a trailing or leading newline.
* **[Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default)** apply.
* You can take input and provide output by **[any standard mean](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)**.
* If possible, please add a testing link to your submission. I will upvote any answer that shows golfing efforts and has an explanation.
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes ***in every language*** wins!
  
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 24 bytes
```
ＦＮ«↶§7117ι×⁺#× ﹪ι²⁻Ｉθ¹»#
```
[Try it online!](https://tio.run/##AT0Awv9jaGFyY29hbP//77ym77yuwqvihrbCpzcxMTfOucOX4oG6I8OXIO@5qs65wrLigbvvvKnOuMK5wrsj//85 "Charcoal – Try It Online")
-5 thanks to [Neil](https://codegolf.stackexchange.com/users/17602/neil).
AST:
```
Program
├Ｆ: For
│├Ｎ: Input number
│└Program
│ ├↶: Pivot Left
│ │└§: At index
│ │ ├'7117': String '7117'
│ │ └ι: Identifier ι
│ └Print
│  └×: Product
│   ├⁺: Sum
│   │├'#': String '#'
│   │└×: Product
│   │ ├' ': String ' '
│   │ └﹪: Modulo
│   │  ├ι: Identifier ι
│   │  └2: Number 2
│   └⁻: Difference
│    ├Ｉ: Cast
│    │└θ: Identifier θ
│    └1: Number 1
└Print
 └'#': String '#'
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~157~~ 153 bytes
```
n=input()
o,s=q='O '
def p(k,t=q*n+s*(4*n-6)):print(t*n)[k*~-n:][:n*3/2*~-n+1]
p(2)
for i in range(n-2):p(0,i*s+s+o+s*(4*n-7-2*i)+o+s*(2*n+i-2))
n>1>p(5)
```
[Try it online!](https://tio.run/##NY1NCsIwEEb3OUV2nZkkaONPoZBewQOULgRbDYVJ2sSFG68eK@Lywfe@F1/5EdiWws5zfGZAEXRyi6sushK3cZIRZp3dQqwSwZHYnBHbuHrOkImxn@ltuB36lumws19Q9SAiWBRTWKWXnuV65fsIbOxmwl57Siqp8D9sjCWPP7Zbx287FNzVXYQTltJ8AA "Python 2 – Try It Online")
* `n*3/2*~-n+1` is the width of each line: ⌊3n/2⌋ · (n−1) + 1 characters.
* The string `q*n+s*(4*n-6)` represents the top and bottom rows. If we repeat it and slice `[2*(n-1):]` we get the top row; if we slice `[5*(n-1):]` we get the bottom row. Hence the definition of `p` and the calls to `p(2)` and `p(5)`. But since we need the repetition and line-length slicing for all other lines anyway, we reuse `p` in the loop.
* The `i*s+s+o+…` is just a boring expression for the middle rows.
* `n>1>p(5)` will short-circuit if `n≯1`, causing `p(5)` to not get evaluated. Hence, it’s shorthand for `if n>1:p(5)`.
[Answer]
# Mathematica, ~~126~~ ~~125~~ ~~121~~ ~~112~~ ~~104~~ ~~89~~ 86 bytes
```
(m=" "&~Array~{#,#^2-#+1};Do[m[[1[i,#,-i][[j~Mod~4]],j#-#+i+1-j]]="X",{j,#},{i,#}];m)&
```
* `#` is the input number for an anonymous function (ended by the final `&`).
* `m=" "&~Array~{#,#^2-#+1};` makes a matrix of space characters of the right size by filling an array of given dimensions `#,#^2-#+1` with the outputs of the constant anonymous function "output a space" `" "&`.
* `Do[foo,{j,#},{i,#}]` is a pair of nested do loops, where `j` ranges from `1` to `#` and inside of that `i` ranges from `1` to `#`.
* `m[[1[i,#,-i][[j~Mod~4]],j#-#+i+1-j]]="X"` sets the the corresponding part of the matrix to be the character `X` based on `j` and `i`. The `-i` uses negative indexing to save bytes from `#-i+1`. (I forgot to write `Mod[j,4]` as `j~Mod~4` in the original version of this code.) Jenny\_mathy pointed out that we can use the modular residue to index into the list directly (rather than using `Switch`) to save 9 bytes, and JungHwan Min pointed out that we didn't need to use `ReplacePart` since we can set a part of an array and that `1[i,#,-i][[j~Mod~4]]` uses the odd behavior and generality of `[[foo]]` to save bytes over `{1,i,#,-i}[[j~Mod~4+1]]`
* Since [meta established that a list of characters is a string](https://codegolf.meta.stackexchange.com/questions/2214/whats-a-string) (as [JungHwan Min pointed out](https://codegolf.stackexchange.com/questions/136660/build-a-semi-zigzag/136689#comment334433_136696)) we don't need to map any function across the rows of the matrix of characters since it's already a list of "string"s.
You can test this out in the [Wolfram Cloud sandbox](http://sandbox.open.wolframcloud.com) by pasting code like the following and hitting Shift+Enter or the numpad Enter:
```
(m=" "&~Array~{#,#^2-#+1};Do[m[[1[i,#,-i][[j~Mod~4]],j#-#+i+1-j]]="X",{j,#},{i,#}];m)&@9//MatrixForm
```
[Answer]
## C++, ~~321~~ 234 bytes
-87 bytes thanks to Zacharý
```
#include<vector>
#include<string>
auto z(int n){std::vector<std::string>l;l.resize(n,std::string(n*n+n/2*(n-1),32));l[0][0]=79;int i=0,j,o=0;for(;i<n;++i)for(j=1;j<n;++j)l[i%4?i%4-1?i%4-2?0:n-j-1:n-1:j][i*n+j-i+(o+=i%2)]=79;return l;}
```
Returns a vector of strings
[Answer]
# Mathematica, 179 bytes
```
Rotate[(c=Column)@(t=Table)[{c@(a=Array)[" "~t~#<>(v="o")&,z,0],c@t[t[" ",z-1]<>v,z-1],c@a[t[" ",z-2-#]<>v&,z-1,0],c@t[v,z-Boole[!#~Mod~4<1]-1]}[[i~Mod~4+1]],{i,0,(z=#)-1}],Pi/2]&
```
edit for @JungHwanMin
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~21~~ ~~20~~ 19 bytes
### Code
Uses the new canvas mode:
```
Fx<)Nè'ONÉúR3212NèΛ
```
Uses the **05AB1E** encoding. [Try it online!](https://tio.run/##MzBNTDJM/f/frcJG0@/wCnV/v8Odh3cFGRsZGgG552b//28GAA "05AB1E – Try It Online")
### Explanation:
```
F                      # For N in range(0, input)
 x<)                   #   Push the array [input, 2 × input - 1]
    Nè                 #   Retrieve the Nth element
      'ONÉúR           #   Push "O" if N is odd, else "O "
            3212Nè     #   Retrieve the Nth element of 3212
                  Λ    #   Write to canvas
```
For input **6**, this gives the following arguments (in the same order) for the canvas:
```
[<num>, <fill>, <patt>]
[6,     'O',     3]
[11,    'O ',    2]
[6,     'O',     1]
[11,    'O ',    2]
[6,     'O',     3]
[11,    'O ',    2]
```
To explain what the canvas does, we pick the first set of arguments from the list above.
The number **6** determines the *length* of the string that will be written into the canvas. The filler is used to write on the canvas, which in this case is `O`. It cyclically runs through the filler string. The direction of the string is determined by the final argument, the direction. The directions are:
```
7  0  1
 \ | /
6- X -2
 / | \
5  4  3
```
This means that the **3** sets the direction to *south-east*, which can also be [tried online](https://tio.run/##MzBNTDJM/f/fTEHJX0nBWOHc7P//AQ).
[Answer]
# [SOGL V0.12](https://github.com/dzaima/SOGL), 36 [bytes](https://github.com/dzaima/SOGL/blob/master/chartable.md)
```
╝.H∫2\?.╝}F2%?№@.┌Ο};1w⁄Hh1ž}.4%1>?№
```
[Try it Here!](https://dzaima.github.io/SOGLOnline/?code=JXUyNTVELkgldTIyMkIyJTVDJTNGLiV1MjU1RCU3REYyJTI1JTNGJXUyMTE2QC4ldTI1MEMldTAzOUYlN0QlM0IxdyV1MjA0NEhoMSV1MDE3RSU3RC40JTI1MSUzRSUzRiV1MjExNg__,inputs=NQ__)
The basic idea is to for each number of the input range choose either adding a diagonal or the horizontal dotted part, in which case it will turn the array around for easier adding on.
Explanation:
```
╝                                     get a diagonal from the bottom-left corner with the length of the input - the starting canvas
 .H∫                        }         for each number in the range [1,inp-1] do, pushing counter
    2\?  }                              if it divides by 2, then
       .╝                                 create another diagonal of the input
          F2%                           push counter % 2
             ?     }                    if that [is not 0]
              №                           reverse the current canvas upside down
               @.┌Ο                       get an alternation of spaces and dashes with the dash amount of the input length
                    ;                   get the canvas on top of the stack
                     1w⁄                get its 1st element length
                        H               decrease it
                         h              swap the bottom 2 items - the canvas is now at the bottom and the current addition ontop
                          1             push 1
                           ž            at 1-indexed coordinates [canvasWidth-1, 1] in the canvas insert the current part made by the Ifs
                             .4%1>?   if input%4 > 1
                                   №    reverse the array vertically
```
If the input of 1 wasn't allowed, then `ο.∫2%?.╝}F2\?№@.┌Ο};1w⁄Hh1ž}.4%1>?№` would work too. If random numbers floating around were allowed `.∫2%?.╝}F2\?№@.┌Ο};1w⁄Hh1ž}.4%1>?№` would work too. If I were not lazy and implemented [`‼`](https://github.com/dzaima/SOGL/blob/master/P5ParserV0_12/data/charDefs.txt#L702), `}F2%?` could be replaced with `‼` for -4 bytes
[Answer]
# Mathematica, ~~106~~ 87 bytes
```
SparseArray[j=i=1;k=#-1;Array[{j+=Im@i;k∣#&&(i*=I);j,#+1}->"o"&,l=k#+1,0],{#,l}," "]&
```
Returns a `SparseArray` object of `String`s. To visualize the output, you can append `Grid@`. Throws an error for case `1`, but it's safe to ignore.
### Explanation
```
j=i=1
```
Set `i` and `j` to 1.
```
k=#-1
```
Set `k` to input - 1.
```
l=k#+1
```
Set `l` to `k*input + 1`
```
Array[ ..., l= ...,0]
```
Iterate `l` times, starting from `0`, incrementing by `1` each time...
---
```
j+=Im@i
```
Add the imaginary component of `i` to `j`...
```
k∣#&&(i*=I)
```
If the current iteration is divisible by `k`, multiply `i` by the imaginary unit...
```
{... j,#+1}->"o"
```
Create a `Rule` object that changes element at position `{j, current iteration + 1}` to `"o"`
---
```
SparseArray[ ...,{#,l}," "]
```
Create a `SparseArray` object using the generated `Rule` objects, with dimension `{input, l}`, using `" "` as blank.
[Try it on Wolfram Sandbox!](https://sandbox.open.wolframcloud.com/)
[Answer]
# [Python 3](https://docs.python.org/3/), ~~228 226 224 215 197~~ 195 bytes
***-11 bytes Thanks to @Mr. Xcoder***
-2 bytes Thanks to @Mr. Xcoder
```
def f(n,s=range):
 x=y=t=c=0;z=[]
 for i in s(n*n-n+2):c+=i%(n-(2<=n))<1;z+=[[x,y]];t=max(t,x);x+=2-c%2;y+=[-1,1][c%4<3]*(c%2)
 return'\n'.join(''.join(' O'[[k,j]in z]for k in s(t))for j in s(n))
```
[Try it online!](https://tio.run/##PY9NboMwEIX3nGI2iJlgqphkFTNn6AFcLyIKrYk6RI4rGS5PTf9WT/Nm8X3vvsT3WU7b9jqMMKKoB4ervA10KSDxwpF7PpqVrStgnAN48AIPlIM0Urd06Wv2JUqDbcdC1Gmz1mxtUotzJvLHNWFUiUyquW36sjVLfjdaaWf78tyd3AFzSwWEIX4GqV6keppmL1j9JTxX1t7U5DJ4dbvD7cchEu3X9GtEtP0Lfk9ArUAf9yH34CXiiJ5UBmQEbV8 "Python 3 – Try It Online")
## Explanation and less-golfed code:
```
def f(n):
 x=y=t=c=0;z=[]                       #initialize everything
 for i in range(n*n-n+2):             #loop n*n-n+2 times which is the numberr of 'o's expected
    c+=i%[~-n,n]<n-1                  #if one cycle has been completed, increase c by 1, if n>1.                                            
    z+=[[x,y]]                        #add [x,y] to z(record the positions of 'o')
    t=max(t,x)                        #trap maximum value of x-coordinate(to be used later while calculatng whole string)
    r=[[2,0],[1,1],[2,0],[1,-1]][c%4] #r gives direction for x and y to move, adjust it as per c i.e. cycles
    x+=r[0];y+=r[1]                   #yield newer values of x and y 
 return '\n'.join(''.join(' o'[[k,j]in z]for k in range(t))for j in range(n)) #place space or 'o' accordingly as per the recorded posititons in z
```
[Answer]
# [Haskell](https://www.haskell.org/), 197 bytes
```
a n c=take(2*n)$cycle$c:" "
r i n x y=take(div(3*n)2*(n-1)+1)$(' '<$[1..i])++(cycle$"O "++(a(2*n-i-3)y)++"O "++(a(n+i-2)x))
z n=take n$(r 0 n 'O' ' '):[r i n ' ' ' '|i<-[1..n-2]]++[r(n-1)n ' ' 'O']
```
[Try it online!](https://tio.run/##RY7BbsIwDIbP5CmsKFJioqC1HCYh@ghb76tyiEoFFsWggjaK9u6d27JNPtlf/v/LIV2PTdsOQwKGurilY@PyJaOp@7ptTL3RoFUHJPQO/cx39OnW8iZfOg4Z@gyNs2C3pspWK4rovZvTugQtSxobA4U19sL@juwp5HhHVA/gqRnYuA5exGVLKQSLm2p223m@aRtGCYc8Ru@rbvrAE5c2DpeO@PZB@0faS6qA3VktppvR74Vor4fzF7BanNLlDSYATuyo1CnRb@AfPptG5WscfgA "Haskell – Try It Online")
*Thanks to @Lynn : fixed the spaces between `O`s on horizontal segments of the zigzag, but it costed a lot of bytes!*
Some explanations:
* `r` is a row of the output: it has the `0 y y y y y 0 x x x 0 y ...` format, the number of `x`and `y` depending on the row and the initial `n`
* for the top row, `x='0'`and `y=' '`
* for the middle rows, `x=' '`and `y=' '`
* for the bottom row, `x=' '`and `y='0'`
* `take(div(3*n)2*(n-1)+1)` cuts an infinite row at the right place
* every output has one top row and one bottom row except when `n=1`: `take n` handles this case.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~155~~ ~~151~~ ~~146~~ 137 bytes
```
m=input()
n=m-1
r=range(n+2)
for L in zip(*[' '*i+'O'+n*' 'for i in(r+[n,m]*~-n+r[-2::-1]+([m,0]*n)[:-1])*m][:1+3*m/2*n]):print''.join(L)
```
[Try it online!](https://tio.run/##NY4xCgIxEEX7nCJgkcxkoyYWSmBvsOABQgoLV0fIbAhroYVXj7uI3X88ePzymu8T@9ZyT1yeswbBfbZO1L5e@HbVbDyIcapykMTyTUVjVFIhGXVWhnHZq6XF6moidznhx7Kp0foQrEtGx9ztEzLEFQFzisGZA@adR04QSiWeldo@piUxQNv8e/L3wHUnCGIjR03Qjl8 "Python 2 – Try It Online")
]