## Bulge calculation

Discussion forum for contributors and developers who are using the QCAD ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

### Bulge calculation

I always wanted to ask in the forum on how to simplify this. To me the calculation of the bulge seems overly complicated. Maybe because I only used the most basic triangle functions (ancient greek ones), as I am not good at geometry. One will not find the formula itself on the afralisp page, but everything to understand, what it does. Here's the drawing:

Such a hole is used eg. to fasten certain fixtures to metal sheets. The fixture is round with a thread in order to take on a nut. It is also square, in order to not turn once inserted.

The code calculates the points counterclockwise and pushes them to array SG. Then it calculates the bulge for the rounded corners and pushes it to array SB. There is only one argument, the center of both square and circle.

Code: Select all

var c = new RVector(0, 0);

/* Ein Kreis und ein Rechteck übereinander
* der Kreis schneidet die rechten Ecken ab
* Schnittpunkte gegen Uhrzeigersinn
* bei zirka zwei Uhr beginnend
*/
var rk = 19; // Radius Kreis
var sq = 17.5; // halbe Seitenlänge Quadrat
var sv = Math.sqrt(Math.pow(rk, 2) - Math.pow(sq, 2)); // Versatz Schnittpunkt
var SG = []; // Punkte der Stanzlinie
SG.push(new RVector(c.x + sq, c.y + sv)); //  2 H
SG.push(new RVector(c.x + sv, c.y + sq)); //  1 H
SG.push(new RVector(c.x - sv, c.y + sq)); // 11 H
SG.push(new RVector(c.x - sq, c.y + sv)); // 10 H
SG.push(new RVector(c.x - sq, c.y - sv)); //  8 H
SG.push(new RVector(c.x - sv, c.y - sq)); //  7 H
SG.push(new RVector(c.x + sv, c.y - sq)); //  5 H
SG.push(new RVector(c.x + sq, c.y - sv)); //  4 H
// siehe http://www.afralisp.net/archive/lisp/Bulges1.htm
var bs = Math.sqrt(2 * Math.pow(sq - sv, 2)) / 2; // halbe Sehne vom Kreisbogen
var bh = rk - Math.sqrt(Math.pow(rk, 2) - Math.pow(bs, 2)); // Höhe Ausbuchtung
var bulge = bh / bs; // Tangens: Gegenkathete/Ankathete
var SB = [ bulge, 0, bulge, 0, bulge, 0, bulge, 0 ]; // abwechselnd mit/ohne Beule
this.polylinie(zeichnung, true, SG, SB);

Polylinie is just a convencience function.

Code: Select all

/** Eine Linie über mehrere Punkte zeichnen, optional Linie schließen.
* @param {Object} zeichnung die Zeichnung
* @param {boolean} geschlossen Linie rundum?
* @param {Array} punkte Liste von Vektoren
* @param {Array} beulen Liste von Ausbuchtungen
*/
MyScript.prototype.polylinie = function(zeichnung, geschlossen, punkte, beulen) {
var polylinie = new RPolylineData();
for (var p = 0; p < punkte.length; p++) {
if (beulen) {
polylinie.appendVertex(punkte[p], beulen[p]);
} else {
polylinie.appendVertex(punkte[p]);
}
}
polylinie.setClosed(geschlossen);
var entity = new RPolylineEntity(zeichnung.document, polylinie);