A class of classes or a class of arrays? How to handle many ropes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

A class of classes or a class of arrays? How to handle many ropes?

I am handling a rope in my code (drawing heart) but I would like to have many. I have made a class for points on the rope and differentiate arr[0], the point leading the rope from its trails. I have global variables to define its path. But I want to have several ropes, so I would need variables local to the class or replication them for each node? How can I have the object be the whole array of nodes, rather than having an array of objects? Or can I have a local variables for the class but not for each object? https://code.sololearn.com/WAbyLL2qNt89/?ref=app

18th Jan 2021, 8:43 AM
bell
bell - avatar
8 Answers
+ 6
bell you can totally have a rope class and a node class. You can have a `nodes` property inside your `rope` class, which will actually be an array of nodes. I saw in your code that you are creating the nodes for your rope (I didn't quite get how). By having a rope class, you can have a method that will create the nodes for that specific rope, instead of having a global function that fills a global array with nodes. Is this the flow you're looking for in your program https://code.sololearn.com/c4sz9vx5O8OE/?ref=app
19th Jan 2021, 1:20 PM
XXX
XXX - avatar
+ 4
bell I think I might know what you're trying to ask, but the question(s) are not quite clear enough to know for certain. The phrasing is what's throwing me off. Neither a "class of classes" or a "class of arrays" are worded in a way that makes clear sense. The following question is also quite ambiguous... "How can I have the object be the whole array of nodes, rather than having an array of objects?" And even this doesn't read very clear: "Or can I have a local variable for the class but not for each object?" Local to what? Are you referring to a local variable in the class? Or are you referring to a local variable in a function that is an instance of a class? Are you referring to a static (class) variable vs instance (instance) variable? I'm just trying to understand what you're asking so I don't invest time into answering the wrong question. 😉 I think there is a lot of context missing in your question that is clear to you but not so much to the reader.
18th Jan 2021, 5:03 PM
David Carroll
David Carroll - avatar
+ 4
David Carroll thank you! My question is not clear because I have indeed many. Changing perspective: I know the result i want, I don't know what is doable and good practice. I have made a rope (a group of particles where one of them directs motion and links among particles constraining their behaviour) and the code I post is where I now stand. A heart class, each particle in the rope array an object of the class. (I also tried an implementation with a base class for nodes and s derived class for the more complex driving node) but I want to have a variable number of ropes with variable particle composition (number of nodes) and variable properties comon to each rope. My question is how would I make this if I was a js programer.
18th Jan 2021, 6:37 PM
bell
bell - avatar
+ 4
David Carroll I though one possibility would be to have a class rope and a class node. The rope objects made of nodes and managing them, but I don't know if this exists in js and I could not find it. Another would be to drop the idea of node class and instead have directly a rope class, saving particle coordinates in an array and having what now are global variables (reference point, size of graph, limits, etc) as rope object variables. I do not know what is the proper way to scale up things.
18th Jan 2021, 6:44 PM
bell
bell - avatar
+ 4
XXX yes I want to try such a flow! Thank you for the demo. Additionally, I am asking if handling ropes this way is reasonable and good practice
19th Jan 2021, 1:38 PM
bell
bell - avatar
+ 4
bell I think so atleast. I don't see any reason why this would be bad practise. Maybe David Carroll can confirm? Although I do have one suggestion. Making nodes a linked list instead of an array would be much more reasonable in my opinion. Arrays are fine and don't really affect the performance of the program. But I think doing `this.x += (this.nextNode.x - this.x) / (idx + 1)` is much more sef-explanatory and readable than `this.x += (rope[idx-1].x-this.x) /(idx+1) ;` (line 77)
19th Jan 2021, 2:13 PM
XXX
XXX - avatar
+ 4
I want to thank both of you for your posts once again. David Carroll for giving me systematic question I can now explore (my text was unclear due to my confusion rather than my English). XXX for helping me choose and try with his example code. I would mark both answers as best, but I have chosen on the hope of drawing a future answer from @David Carrol to one of his questions! The code has now two ropes! (ugly, so I wont post it but I can do new things now ) https://code.sololearn.com/WA60xW6D7r14/?ref=app
19th Jan 2021, 7:17 PM
bell
bell - avatar
+ 3
bell For clarification... Are you asking about having a class with explicitly named properties vs a single named property containing an array of objects? Or are you asking about creating a class that extends the Array object and acts as an array itself? Or are you trying to find a better way to decouple the dependency of Heart.update(idx) method from having to locate the previous Heart object in the rope array? These are all very different questions that may or may not address the actual question you're trying to ask. 😉
18th Jan 2021, 5:05 PM
David Carroll
David Carroll - avatar