# 2020-04-06 | IA | CGPoint Array to Double Array
###### tags: `swift` `ia` `gobelins`
## Import
```
import Foundation
```
## Fonctions
### GetCoord
```swift
func getCoord(pos:[CGPoint]) -> [Double] {
var values = [Double]()
var coordX = [Double]()
var coordY = [Double]()
for item in pos {
coordX.append(Double(item.x))
coordY.append(Double(item.x))
}
values = coordX + coordY
return values
}
```
### GetCoordCouples
```swift
func getCoordCouples(pos:[CGPoint]) -> [Double] {
var values = [Double]()
for item in pos {
values.append(Double(item.x))
values.append(Double(item.y))
}
return values
}
```
### FormatArray
```swift
func formatArray(nbPos:Int, posArray:[Double]) -> [Double]{
var finalArray = [Double]()
for i in 0..<nbPos{
if i < posArray.count {
finalArray.append(posArray[i])
}
else{
finalArray.append(0.0)
}
}
return finalArray
}
```
### NormalizeArray
```swift
func normalizeArray(values:[Double]) -> [Double] {
let min = values.min()!
let max = values.max()!
var finalArray = [Double]()
if min == max {
for item in values {
finalArray.append(1.0 / Double(values.count))
}
}else{
for i in 0..<values.count{
finalArray.append((values[i] - min)/(max - min))
}
}
return finalArray
}
```
### ConvertAndNormalize
```swift
enum CoordOrganization {
case pair,seq
}
func convertAndNormalize(coords:[CGPoint], org:CoordOrganization, n:Int) -> [Double] {
var values = [Double]()
switch org {
case .pair:
values = getCGPointToDoubleCouple(pos:coords)
case .seq:
values = getCGPointToDouble(pos:coords)
default:
fatalError("Unsupported")
}
values = formatArray(nbPos:n,posArray:values)
values = normalizeArray(values:values)
return values
}
```
## Call
```
var pos = [CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9)]
print("Values avec les couples de coordonnées, tableau de taille 5 :")
print(convertAndNormalize(coords:pos,org:CoordOrganization.pair,n:5))
print("Values avec les x puis les y, tableau de taille 22 :")
print(convertAndNormalize(coords:pos,org:CoordOrganization.seq,n:22))
```
## Result
```
Values avec les couples de coordonnées, tableau de taille 5 :
[0.0, 0.2, 0.4, 1.0, 0.8]
Values avec les x puis les y, tableau de taille 22 :
[0.0, 0.5, 1.0, 0.25, 1.0, 0.0, 0.5, 1.0, 0.25, 1.0, 0.0, 0.5, 1.0, 0.25, 1.0, 0.0, 0.5, 1.0, 0.25, 1.0, 0.0, 0.5]
```