Graph 如何使用swift在sprit工具包中绘制网格拓扑

Graph 如何使用swift在sprit工具包中绘制网格拓扑,graph,sprite-kit,mesh,topology,Graph,Sprite Kit,Mesh,Topology,我正在尝试使用swift在sprite工具包中绘制网格拓扑(图形)。我是雪碧工具包的新手。请给出任何建议或示例代码 所以,正如我在评论中提到的,我不知道如何制作一个完美的地形算法,但是我确实想出了一些可以复制你的图片的东西 基本上,您可以添加一组图作为节点,然后使用.position属性作为使用CGPath绘制的直线的起点和终点。从该路径,您可以创建SKShapeNode(路径:CGPath) 我还在这里添加了一个使用委托的自定义按钮,但它与地形的实际“胆量”完全不同。这只是一个按钮 // Ov

我正在尝试使用swift在sprite工具包中绘制网格拓扑(图形)。我是雪碧工具包的新手。请给出任何建议或示例代码

所以,正如我在评论中提到的,我不知道如何制作一个完美的地形算法,但是我确实想出了一些可以复制你的图片的东西

基本上,您可以添加一组图作为节点,然后使用
.position
属性作为使用
CGPath
绘制的直线的起点和终点。从该路径,您可以创建
SKShapeNode(路径:CGPath)

我还在这里添加了一个使用委托的自定义按钮,但它与地形的实际“胆量”完全不同。这只是一个按钮

// Overly complex way of creating a custom button in SpriteKit:
protocol DrawLinesDelegate: class { func drawLines() }

// Clickable UI element that will draw our lines:
class DrawLinesButton: SKLabelNode {
  
  weak var drawLinesDelegate: DrawLinesDelegate?
  
  init(text: String, drawLinesDelegate: DrawLinesDelegate) {
    super.init(fontNamed: "Chalkduster")
    self.drawLinesDelegate = drawLinesDelegate
    self.text = text
    isUserInteractionEnabled = true
  }
  
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print(drawLinesDelegate)
    
    drawLinesDelegate?.drawLines()
  }
  required init?(coder aDecoder: NSCoder) { fatalError("") }
  override init() { super.init() }
};


class GameScene: SKScene, DrawLinesDelegate {
  
  var plots = [SKShapeNode]()
  
  // var lines = [SKShapeNode]()  // This may be useful in a better algorithm.
  
  var nodesDrawnFrom = [SKShapeNode]()
  
  var drawLinesButton: DrawLinesButton?
  
  func drawLine(from p1: CGPoint, to p2: CGPoint) {
    
    let linePath = CGMutablePath()
    linePath.move(to: p1)
    linePath.addLine(to: p2)
    
    let line = SKShapeNode(path: linePath)
    line.strokeColor = .red
    line.lineWidth   = 5
    // lines.append(line) // Again, may be useful in a better algo.
    addChild(line)
  }
  
  func drawLines() {
    
    // Remove all lines: // Again again, may be useful in a better algorithm.
    /*
     for line in lines {
     line.removeFromParent()
     lines = []
     }
     */
    
    // The plot that we will draw from:
    var indexNode = SKShapeNode()
    
    // Find indexNode then draw from it:
    for plot in plots {
      
      // Find a new node to draw from (the indexNode):
      if nodesDrawnFrom.contains(plot) {
        continue
      } else {
        indexNode = plot
      }
      
      // Draw lines to every other node (from the indexNode):
      for plot in plots {
        if plot === indexNode {
          continue
        } else {
          drawLine(from: indexNode.position, to: plot.position)
          nodesDrawnFrom.append(indexNode)
        }
      }
    }
  }
  
  func addNode(at location: CGPoint) {
    let plot = SKShapeNode(circleOfRadius: 50)
    plot.name = String(describing: UUID().uuid)
    plot.zPosition += 1
    plot.position = location
    plot.fillColor = .blue
    
    plots.append(plot)
    addChild(plot)
  }
  
  override func didMove(to view: SKView) {
    drawLinesButton = DrawLinesButton(text: "Draw Lines", drawLinesDelegate: self)
    drawLinesButton!.position.y = frame.minY + (drawLinesButton!.frame.size.height / 2)
    addChild(drawLinesButton!)
  }
  
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let location = touches.first!.location(in: self)
    addNode(at: location)
  }
}
//在SpriteKit中创建自定义按钮的方式过于复杂:
协议DrawLinesDelegate:类{func drawLines()}
//可单击的UI元素将绘制我们的线条:
类Drawlines按钮:SKLabelNode{
弱var drawLinesDelegate:drawLinesDelegate?
初始化(文本:字符串,drawLinesDelegate:drawLinesDelegate){
super.init(名为“粉笔掸子”)
self.drawLinesDelegate=drawLinesDelegate
self.text=文本
isUserInteractionEnabled=true
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
打印(抽绳复制)
drawLinesDelegate?.drawLines()
}
必需的初始化?(编码器aDecoder:NSCoder){fatalError(“”}
重写init(){super.init()}
};
类游戏场景:SKScene,DrawLinesDelegate{
变量图=[SKShapeNode]()
//var lines=[SKShapeNode]()//这在更好的算法中可能很有用。
var nodesDrawnFrom=[SKShapeNode]()
var drawLinesButton:drawLinesButton?
func绘制线(从p1:CGPoint到p2:CGPoint){
设linePath=CGMutablePath()
linePath.move(到:p1)
linePath.addLine(到:p2)
let line=SKShapeNode(路径:linePath)
line.strokeColor=.red
line.lineWidth=5
//line.append(line)//同样,在更好的算法中可能很有用。
addChild(行)
}
func抽绳(){
//再次删除所有行,可能对更好的算法有用。
/*
排队{
行。removeFromParent()
行=[]
}
*/
//我们将从中得出的情节:
var indexNode=SKShapeNode()
//找到indexNode,然后从中绘制:
一幅一幅地画{
//查找要从中绘制的新节点(indexNode):
如果nodesDrawnFrom.contains(绘图){
持续
}否则{
索引节点=绘图
}
//每隔一个节点(从indexNode)绘制一条线:
一幅一幅地画{
如果绘图===索引节点{
持续
}否则{
绘制线(从:indexNode.position到:plot.position)
nodesDrawnFrom.append(indexNode)
}
}
}
}
func addNode(位置:CGPoint){
let plot=SKShapeNode(圆半径:50)
plot.name=字符串(描述:UUID().UUID)
plot.zPosition+=1
plot.position=位置
plot.fillColor=.blue
绘图。追加(绘图)
addChild(绘图)
}
覆盖func didMove(到视图:SKView){
drawLinesButton=drawLinesButton(文本:“绘制线”,drawLinesDelegate:self)
drawLinesButton!.position.y=frame.minY+(drawLinesButton!.frame.size.height/2)
addChild(drawLinesButton!)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
让location=touchs.first!.location(在:self中)
addNode(位于:位置)
}
}

不完全算法: 在这里,您可以看到,当您有中点时,会有多条线从一条线绘制到另一条线(这是阻挡直线的东西):

您需要在算法中添加另一个完整的部分来检查这一点

另一个需要注意的重要事项是,SKShapeNode()的性能非常差,最好将所有这些都转换为spritenode,或者将整个场景稍微渲染到静态纹理上


但是,将它们全部作为ShapeNodes会给您带来最大的灵活性,并且在这里最容易解释。

我正在为您准备一个粗略的答案,但实际的
drawLines()
算法我不知道。我想你知道在地形图上画线的算法吧?我刚才做的是一个非常基本和不完美的。嘿,我的答案+项目解决方案对你有用吗?如果没有,请让我知道,以便我可以尝试更新它。谢谢你宝贵的答复。在您的代码中,当我点击屏幕时,只有我可以绘制节点。我正在寻找完整的通用平均负载拓扑,具有10个节点并连接到特定节点。我仍然在寻找解决办法。