double distance = start.distance(worldCoordTo);
int numberofbonds = (int)(distance / (bondLength*.8660254)); // constant is sqrt(3)/4
if(numberofbonds>0){
//add start atom
IAtomContainer phantoms = getBuilder().newInstance(IAtomContainer.class);
IAtom startAtom;
if(source==null){
startAtom = getBuilder().newInstance(IAtom.class, chemModelRelay.getController2DModel().getDrawElement(), start );
}else{
startAtom=source;
}
phantoms.addAtom(startAtom);
//make atoms and bonds as needed
for(int i=0;i<numberofbonds;i++){
IAtom nextAtom = getBuilder().newInstance(IAtom.class, chemModelRelay.getController2DModel().getDrawElement(), new Point2d(startAtom.getPoint2d().x+bondLength, startAtom.getPoint2d().y));
phantoms.addAtom(nextAtom);
phantoms.addBond(getBuilder().newInstance(IBond.class, startAtom, nextAtom, IBond.Order.SINGLE));
startAtom = nextAtom;
}
// The algorithm is 1. calc point a bondlength away in the mouse direction
// 2. calc point rotating the above 30° down, then the same 30° up
// 3. chain the points alternatively
Point2d point = new Point2d(worldCoordTo);
Point2d center = new Point2d(start);
double dx = (point.x - center.x)/numberofbonds;
double dy = (point.y - center.y)/numberofbonds;
double angle = Math.PI / 6;
double cosangle = Math.cos(angle);
double sinangle = Math.sin(angle);
double firstx = dx*cosangle - dy*sinangle;
double firsty = dx*sinangle + dy*cosangle;
double secx = dx*cosangle + dy*sinangle;
double secy = -dx*sinangle + dy*cosangle;
Point2d p1 = new Point2d(start);
phantoms.getAtom(0).setPoint2d(p1);
double currx = p1.x;
double curry = p1.y;
for(int i=1; i<phantoms.getAtomCount(); i++){
Point2d p = new Point2d(currx,curry);
if(i % 2 == 1){
p.x += firstx;
p.y += firsty;
}else{
p.x += secx;
p.y += secy;
}
currx = p.x;
curry = p.y;
phantoms.getAtom(i).setPoint2d(p);
}
chemModelRelay.setPhantoms(phantoms);
chemModelRelay.setPhantomText(""+phantoms.getAtomCount(), worldCoordTo);
IAtom closestAtom = chemModelRelay.getClosestAtom(phantoms.getAtom(phantoms.getAtomCount()-1));
chemModelRelay.getRenderer().getRenderer2DModel().getMerge().remove(merge);
merge = (IAtom) getHighlighted(phantoms.getAtom(phantoms.getAtomCount()-1).getPoint2d(), closestAtom);
if(merge!=null){
chemModelRelay.getRenderer().getRenderer2DModel().getMerge().put(merge,phantoms.getAtom(phantoms.getAtomCount()-1));
chemModelRelay.getPhantoms().getConnectedBondsList(phantoms.getAtom(phantoms.getAtomCount()-1)).get(0).setAtom(merge,1);