import Foundation
let rows = 20
let cols = 20
var q : Array<String>=[]
func gameInit (color: String){
for_in 0..<rows {
for_in 0. .<cols{
g.append (color)
}
}
}
func display () {
for i in 0.. ‹rows {
for j in 0..<cols {
print (g[i*cols+j],separator: ""terminator: "")
}
print ()
}
print ("-------分割线-------")
}
func adaLine (source: (x: Int, y: Int), destination: (x: Int, y: Int) , color: String){
if (source==destination){
return
}
else if(source.x==destination.x) {
for hewY in source.y...destination.y {
g[cols * newY + source.x) = color
}
}
e1se if(source.y==destination.y) {
for newX in source.x...destinaticn.x {
g[cols * aource,y + newX ] = sclor
}
}
else if (sourve.x<destination.x) {
var dx:Float = 0, dy:Float = 0
var newY : Float = 0
var k : Float = 0
dx = Float (destination.x - source.x)
dy = Float (destination.y - source.y)
k = dy / dx
newY = Float(source.y)
for x in source.x...destination.x{
g[cols * Int(round(Double(newY))) + x] = color
newY += k
}
}
else {
let nDestination = source
let nSource = destination
var dx:Float = 0, dy:Float = 0
var newY : Float = 0
var k : Float = 0
dx = Float(nDestination.x - nSource.x)
dy = Ploat(nDestination.y - nSource.y)
k = dy / dx
newY = Float(nSource.y)
for x in nSource.x...nDestination.x {
g[cols * Int(round(Double(newY))) + x] = color
newY += k
}
}
}
gameInit(color:"——")
display()
ddaLine(source: (2,3), destination: (17,12) , color: "*")
display()