Here I present a simple example of a hybrid shape, combining a curve, generated by the use 4 curveVertex points and two straight lines either side (each defined using two vertex points).

The thick line represents the desired hybrid shape, the fainter line shows the data points required to define the curve between the two inner points (NB:these lines are not usually displayed, but given an insight into how the curve is defined).

` 1:class HybridLine {`

2:

3: private float k; // x coordinate shape center

4: private float t; // y coordinate shape center

5: /**

6: *

7: * @param xpos shape center

8: * @param ypos shape center

9: */

10:

11: HybridLine(float xpos, float ypos) {

12: k = xpos;

13: t = ypos;

14: }

15: /**

16: * default constructor

17: */

18: HybridLine() {

19: k = 0.0f;

20: t = 0.0f;

21: }

22:

23: /**

24: *

25: * @param x plus and minus parabola x coordinate

26: * @param y the y coordinate

27: * @param px plus and minus coordinate of line tangent

28: * @param py the y coordinate of line tangent

29: */

30: void drawCurve(float x, float y, float px, float py) {

31: curveVertex(k - px, t + py); // defines lh tangent

32: curveVertex(k - x, t + y); // is on curve and tangent

33: curveVertex(k + x, t + y); // is on curve and tangent

34: curveVertex(k + px, t + py); // defines rh tangent

35: }

36:

37: /**

38: * @param lx lhs x coordinate

39: * @param ly lhs y coordinate

40: * @param rx rhs x coordinate

41: * @param ry rhs y coordinate

42: */

43: void drawStraight(float lx, float ly, float rx, float ry) {

44: vertex(k + lx, t + ly);

45: vertex(k + rx, t + ry);

46: }

47:

48: /**

49: *

50: * @param x point on curve

51: * @param y point on curve

52: * @param px defines tangent

53: * @param py defines tangent

54: */

55: void drawTangent(float x, float y, float px, float py) {

56: line(k + px, t + py, k + x, t + y);

57: }

58:}

59:

60: void setup() {

61: size(400, 400);

62: float xpos = width / 2;

63: float ypos = height / 2 ;

64: HybridLine hline = new HybridLine(xpos, ypos);

65: strokeWeight(5);

66: noFill();

67: smooth();

68: beginShape();

69: hline.drawStraight(-150, -100, -80, -100);

70: hline.drawCurve(80, -100, 150, 300);

71: hline.drawStraight(80, -100, 150, -100);

72: endShape();

73: strokeWeight(1);

74: hline.drawTangent(-80, -100, -150, 300);

75: hline.drawTangent(80, -100, 150, 300);

76: }

I propose to create a flying saucer shape with a curved dome on the top of a solid shape.

NB: only if you want to complete ('an enclosing') shape should you endShape(CLOSE).

## No comments:

## Post a Comment