Self.parent.parent_?

class LineAgent(IParticle):
def init(self, parent, dir):
IParticle.init(self, parent.pos().cp(dir))

    self.parent = parent
    self.dir = dir
    self.hide()
    self.fric(0.2)

    self.isColliding = False

def update(self):

    if self.isColliding:
        self.del()

    else:
        if self.time() == 0 and self.alive():
            if self.parent != None and self.parent.alive():
                ln = ISpringLine(self, self.parent, 100).clr(255)
                t = -cos(IG.time()*0.06)*0.5+0.5
                ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                if self.parent.parent != None and self.parent.parent.alive():
                    st = IStraightenerCurve(
                        self, self.parent, self.parent.parent).tension(100)
                    st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)

in this code block i didn t find how can i write true self.parent.parent.Anyone help me_?

Just to clarify something: do you intend to use an object-oriented approach that employs inheritance? If so, your code structure strikes me as odd, but maybe I’m confused.

Conceptually, would something like this work better?

class IParticle(object):
    def __init__(self, parent):
        self.x = parent
        self.y = 2

class LineAgent(IParticle):
    def __init__(self, parent, z):
        super(LineAgent, self).__init__(parent)
        self.z = z

la = LineAgent(1, 3)

print(la.x)  # 1
print(la.y)  # 2
print(la.z)  # 3

@tabreturn : I think OP is trying to port one of the IGeo tutorials on “Particle-based Branching Algorithm with Spring Force”.

2 Likes

yes exacttly that is what i m trying to do

i wanna draw a line using with IStraightnerCurve object in my class LineAgent…It requests 3 points or vectors.LineAgent class product a point or vector and use that point production in itself.self is my main class production and self.parent is its parameter as a point or vector and what would be third one_?self.parent.parent syntax does not work how can i produce third one_?

@Mayarch : Please post the full script so people on the forum can investigate your issue.

My guess is that you are not correctly initializating the LineAgent class where a distinction is made between child agents that have a parent attribute (this.parent) and those that don’t have one because they are the very first LineAgents created in setup().

Hence the error when calling parent on agents that don’t have one.

@tabreturn : I’m not sure how super(parent.pos().cp(dir)) (on line 7 below) would translate in Python.

– original class in Java –

class LineAgent extends IParticle{ 
  LineAgent parent;
  boolean isColliding;
  IVec dir;
    
  LineAgent(IParticle parent, IVec dir){
    super(parent.pos().cp(dir));
    if(parent instanceof LineAgent){
      this.parent = (LineAgent)parent;
    }
    isColliding=false;
    hide(); // hide point
    this.dir = dir;
    fric(0.2);
  }

– Python Mode –

class LineAgent(IParticle):
    def __init__(self, parent, dir):
        super(LineAgent, self).__init__(parent, dir) #or IParticle.__init__(self, parent, dir)
        
        #parent.pos().cp(dir) #<-- WHERE should this go ?
        
        if isinstance(parent, LineAgent):
            self.parent = parent
        
        self.isColliding = False
        self.dir = dir
        self.hide()
        self.fric(0.2)
1 Like

exactly where should i use_?because i wanted to try initialized like u but i could not make code work in previous session’s code

add_library(‘igeo’)

def setup():

# fullScreen(IG.GL)

size(480, 360, IG.GL)

IG.top()

IG.blackBG()

for i in range(4):

    LineAgent(IParticle(IRand.pt(-40, -40, 0, 40, 40, 0)

                        ).hide().fric(0.2), IRand.dir(IG.zaxis).len(2))

for i in range(20):

    IAttractor(IRand.pt(-200, -200, 0, 200, 200, 0)

               ).linear(50).intensity(-30)

class LineAgent(IParticle):

def __init__(self, parent, dir):

    IParticle.__init__(self, parent.pos().cp(dir))

    self.parent = parent

    self.dir = dir

    self.hide()

    self.fric(0.2)

    self.isColliding = False

def interact(self, agents):

    if self.time() == 0:  # only in the first time

        for agent in agents:

            if isinstance(agent, LineAgent):

                if agent is not self:

                    if not agent.isColliding and not self.isColliding:

                        intxn = IVec.intersectSegment(

                            self.parent.pos(), self.pos(), agent.parent.pos(), agent.pos())

                        if intxn is not None:  # intersection exists

                            if not intxn.eq(self.parent.pos()):  # not parent agent

                                self.isColliding = True

                                return

def update(self):

    if self.isColliding:

        self.del()

    else:

        if self.time() == 0 and self.alive():

            if self.parent != None and self.parent.alive():

                ln = ISpringLine(self, self.parent, 100).clr(255)

                t = -cos(IG.time()*0.06)*0.5+0.5

                ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                if self.parent.parent != None and self.parent.parent.alive():

                    st = IStraightenerCurve(

                        self, self.parent, self.parent.parent).tension(100)

                    st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)

        if self.time == 4:

            dir2 = self.dir.cp()

            angle = PI*0.12

            if IRand.pct(50):

                self.dir.rot(angle)

                dir2.rot(-angle)

            else:

                self.dir.rot(-angle)

                dir2.rot(angle)

            LineAgent(self, self.dir)

            if IRand.pct(20):

                LineAgent(self, dir2)

thats my code but i didn t find right way to make it work as you can see!!!

Just the same code reposted w/ better formatting:

add_library('igeo')

def setup():
    size(480, 360, IG.GL)
    
    IG.top()
    IG.blackBG()

    for i in range(4):
        LineAgent(IParticle(IRand.pt(-40, -40, 0, 40, 40, 0)
        ).hide().fric(0.2), IRand.dir(IG.zaxis).len(2))

    for i in range(20):
        IAttractor(IRand.pt(-200, -200, 0, 200, 200, 0)
        ).linear(50).intensity(-30)


class LineAgent(IParticle):
    def __init__(self, parent, dir):
        IParticle.__init__(self, parent.pos().cp(dir))
        self.parent = parent
        self.dir = dir
        self.hide()
        self.fric(.2)
        self.isColliding = False


    def interact(self, agents):
        if self.time() == 0:  # only in the first time

            for agent in agents:
                if isinstance(agent, LineAgent) and agent is not self:
                    if not agent.isColliding and not self.isColliding:
                        intxn = IVec.intersectSegment(
                            self.parent.pos(), self.pos(),
                            agent.parent.pos(), agent.pos())

                        # intersection exists and not parent agent
                        if intxn and not intxn.eq(self.parent.pos()):
                                    self.isColliding = True
                                    return


    def update(self):
        if self.isColliding:
            self.del()
        else:
            if self.time() == 0 and self.alive():
                if self.parent and self.parent.alive():
                    ln = ISpringLine(self, self.parent, 100).clr(255)
                    t = -cos(IG.time()*0.06)*0.5+0.5
                    ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                    # self.parent.parent is an IObject; got no method alive():
                    if self.parent.parent and self.parent.parent.alive():
                        st = IStraightenerCurve(
                            self, self.parent, self.parent.parent).tension(100)
                        st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)
    
            if self.time == 4:
                dir2 = self.dir.cp()
                angle = PI*0.12

                if IRand.pct(50):
                    self.dir.rot(angle)
                    dir2.rot(-angle)
                else:
                    self.dir.rot(-angle)
                    dir2.rot(angle)

                LineAgent(self, self.dir)

                if IRand.pct(20):
                    LineAgent(self, dir2)
3 Likes

With IGeo there shouldn’t be a draw() method I think.

1 Like

acording to duration time, IGeo executes update method in my class then with interact method check relationship new produced particle with old executed particles.

I’m pretty sure it is the opposite: interact()update()

The interact() method runs first, looking for collisions (isColliding), then the update() method decides whether to spawn a new child agent or not based on the boolean result (True or False).

That is actually one of the main reasons why you should have an error appearing in the interact() method first rather than in the update() method (cf. your sketch).

Again, make sure that the attribute parent in the constructor of the LineAgent() object is conditioned by the type of its parent:

– Java –

if(parent instanceof LineAgent){
      this.parent = (LineAgent)parent;
    }

– Python –

if isinstance(parent, LineAgent):
    self.parent = parent

For the rest (the error itself), I’m afraid I can’t help, really sorry. I’ve asked Satoru Sugihara for some insights on the IGeo repo, let’s hope he’ll respond.

3 Likes