Skip to content

Commit

Permalink
#526 180-degree angle in case of two connected double/triple bonds (#740
Browse files Browse the repository at this point in the history
)

* #526 180-degree angle in case of two connected double/triple bonds

* Fix code style issue
  • Loading branch information
AleksandraIvleva authored Sep 2, 2021
1 parent c447231 commit 65e731e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/ketcher-react/src/script/editor/actions/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function fromBondAddition(
): [Action, number, number, number] {
// eslint-disable-line
if (end === undefined) {
const atom = atomForNewBond(restruct, begin)
const atom = atomForNewBond(restruct, begin, bond)
end = atom.atom
pos = atom.pos
}
Expand Down
22 changes: 19 additions & 3 deletions packages/ketcher-react/src/script/editor/actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { Vec2 } from 'ketcher-core'
import closest from '../shared/closest'
import { difference } from 'lodash'
import { Bond } from 'ketcher-core'

export function atomGetAttr(restruct, aid, name) {
return restruct.molecule.atoms.get(aid)[name]
Expand Down Expand Up @@ -56,10 +57,15 @@ export function structSelection(struct) {
}

// Get new atom id/label and pos for bond being added to existing atom
export function atomForNewBond(restruct, id) {
export function atomForNewBond(restruct, id, bond) {
// eslint-disable-line max-statements
const neighbours = []
const pos = atomGetPos(restruct, id)
const prevBondId = restruct.molecule.findBondId(
id,
restruct.molecule.atomGetNeighbors(id)[0].aid
)
const prevBondType = restruct.molecule.bonds.get(prevBondId).type

restruct.molecule.atomGetNeighbors(id).forEach(nei => {
const neiPos = atomGetPos(restruct, nei.aid)
Expand Down Expand Up @@ -132,8 +138,18 @@ export function atomForNewBond(restruct, id) {
}
}

angle =
maxAngle / 2 + Math.atan2(neighbours[maxI].v.y, neighbours[maxI].v.x)
if (
neighbours.length === 1 &&
prevBondType === bond.type &&
(bond.type === Bond.PATTERN.TYPE.DOUBLE ||
bond.type === Bond.PATTERN.TYPE.TRIPLE)
) {
const prevBondAngle = restruct.molecule.bonds.get(prevBondId).angle
angle = (prevBondAngle * Math.PI) / 180
} else {
angle =
maxAngle / 2 + Math.atan2(neighbours[maxI].v.y, neighbours[maxI].v.x)
}

v = v.rotate(angle)
}
Expand Down

0 comments on commit 65e731e

Please sign in to comment.