<commit_before>package toxi.physics2d;

import java.util.ArrayList;
import java.util.List;

import toxi.geom.Vec2D;

/**
 * Utility builder/grouping/management class to connect a set of particles into
 * a physical string/thread. Custom spring types can be used by subclassing this
 * class and overwriting the
 * {@link #createSpring(VerletParticle2D, VerletParticle2D, float, float)}
 * method.
 */
public class ParticleString2D {

    protected VerletPhysics2D physics;
    public List<VerletParticle2D> particles;
    public List<VerletSpring2D> links;

    /**
     * Takes a list of already created particles connects them into a continuous
     * string using springs.
     * 
     * @param physics
     *            physics engine instance
     * @param plist
     *            particle list
     * @param strength
     *            spring strength
     */
    public ParticleString2D(VerletPhysics2D physics,
            List<VerletParticle2D> plist, float strength) {
        this.physics = physics;
        particles = new ArrayList<VerletParticle2D>(plist);
        links = new ArrayList<VerletSpring2D>(particles.size() - 1);
        VerletParticle2D prev = null;
        for (VerletParticle2D p : particles) {
            physics.addParticle(p);
            if (prev != null) {
                VerletSpring2D s =
                        createSpring(prev, p, prev.distanceTo(p), strength);
                links.add(s);
                physics.addSpring(s);
            }
            prev = p;

        }
    }

    /**
     * Creates a number of particles along a line and connects them into a
     * string using springs.
     * 
     * @param physics
     *            physics engine
     * @param pos
     *            start position
     * @param step
     *            step direction & distance between successive particles
     * @param num
     *            number of particles
     * @param mass
     *            particle mass
     * @param strength
     *            spring strength
     */
    public ParticleString2D(VerletPhysics2D physics, Vec2D pos, Vec2D step,
            int num, float mass, float strength) {
        this.physics = physics;
        particles = new ArrayList<VerletParticle2D>(num);
        links = new ArrayList<VerletSpring2D>(num - 1);
        float len = step.magnitude();
        VerletParticle2D prev = null;
        for (int i = 0; i < num; i++) {
            VerletParticle2D p = new VerletParticle2D(pos.copy(), mass);
            particles.add(p);
            physics.particles.add(p);
            if (prev != null) {
                VerletSpring2D s = createSpring(prev, p, len, strength);
                links.add(s);
                physics.addSpring(s);
            }
            prev = p;
            pos.addSelf(step);
        }
    }

    /**
     * Removes the entire string from the physics simulation, incl. all of its
     * particles & springs.
     */
    public void clear() {
        for (VerletSpring2D s : links) {
            physics.removeSpringElements(s);
        }
        particles.clear();
        links.clear();
    }

    /**<commit_msg>refactored, now using an internal copy of the pos parameter vector for linear string constructor so that original doesn't get changed<commit_after>package toxi.physics2d;

import java.util.ArrayList;
import java.util.List;

import toxi.geom.Vec2D;
import toxi.physics.VerletSpring;

/**
 * Utility builder/grouping/management class to connect a set of particles into
 * a physical string/thread. Custom spring types can be used by subclassing this
 * class and overwriting the
 * {@link #createSpring(VerletParticle2D, VerletParticle2D, float, float)}
 * method.
 */
public class ParticleString2D {

    protected VerletPhysics2D physics;
    public List<VerletParticle2D> particles;
    public List<VerletSpring2D> links;

    /**
     * Takes a list of already created particles connects them into a continuous
     * string using springs.
     * 
     * @param physics
     *            physics engine instance
     * @param plist
     *            particle list
     * @param strength
     *            spring strength
     */
    public ParticleString2D(VerletPhysics2D physics,
            List<VerletParticle2D> plist, float strength) {
        this.physics = physics;
        particles = new ArrayList<VerletParticle2D>(plist);
        links = new ArrayList<VerletSpring2D>(particles.size() - 1);
        VerletParticle2D prev = null;
        for (VerletParticle2D p : particles) {
            physics.addParticle(p);
            if (prev != null) {
                VerletSpring2D s =
                        createSpring(prev, p, prev.distanceTo(p), strength);
                links.add(s);
                physics.addSpring(s);
            }
            prev = p;

        }
    }

    /**
     * Creates a number of particles along a line and connects them into a
     * string using springs.
     * 
     * @param physics
     *            physics engine
     * @param pos
     *            start position
     * @param step
     *            step direction & distance between successive particles
     * @param num
     *            number of particles
     * @param mass
     *            particle mass
     * @param strength
     *            spring strength
     */
    public ParticleString2D(VerletPhysics2D physics, Vec2D pos, Vec2D step,
            int num, float mass, float strength) {
        this.physics = physics;
        particles = new ArrayList<VerletParticle2D>(num);
        links = new ArrayList<VerletSpring2D>(num - 1);
        float len = step.magnitude();
        VerletParticle2D prev = null;
        pos = pos.copy();
        for (int i = 0; i < num; i++) {
            VerletParticle2D p = new VerletParticle2D(pos.copy(), mass);
            particles.add(p);
            physics.particles.add(p);
            if (prev != null) {
                VerletSpring2D s = createSpring(prev, p, len, strength);
                links.add(s);
                physics.addSpring(s);
            }
            prev = p;
            pos.addSelf(step);
        }
    }

    /**
     * Removes the entire string from the physics simulation, incl. all of its
     * particles & springs.
     */
    public void clear() {
        for (VerletSpring2D s : links) {
            physics.removeSpringElements(s);
        }
        particles.clear();
        links.clear();
    }

    /**
{'fraction_non_alphanumeric': 0.08774113254511512, 'fraction_numerical': 0.009334163036714374, 'mean_word_length': 1.86369710467706, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100523', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1770, 'n_words': 508}