You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ellipse.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xdgf.usermodel.section.geometry;
  16. import java.awt.geom.AffineTransform;
  17. import java.awt.geom.Ellipse2D;
  18. import java.awt.geom.Path2D;
  19. import org.apache.poi.ooxml.POIXMLException;
  20. import org.apache.poi.xdgf.usermodel.XDGFCell;
  21. import org.apache.poi.xdgf.usermodel.XDGFShape;
  22. import com.microsoft.schemas.office.visio.x2012.main.CellType;
  23. import com.microsoft.schemas.office.visio.x2012.main.RowType;
  24. public class Ellipse implements GeometryRow {
  25. Ellipse _master;
  26. // x coordinate of center point
  27. Double x;
  28. // y coordinate of center point
  29. Double y;
  30. // x coordinate of first point on ellipse
  31. Double a;
  32. // y coordinate of first point on ellipse
  33. Double b;
  34. // x coordinate of second point on ellipse
  35. Double c;
  36. // y coordinate of second point on ellipse
  37. Double d;
  38. Boolean deleted;
  39. // TODO: support formulas
  40. public Ellipse(RowType row) {
  41. if (row.isSetDel())
  42. deleted = row.getDel();
  43. for (CellType cell : row.getCellArray()) {
  44. String cellName = cell.getN();
  45. if (cellName == null) {
  46. throw new POIXMLException("Invalid cell '" + cellName
  47. + "' in Ellipse row");
  48. }
  49. switch (cellName) {
  50. case "X":
  51. x = XDGFCell.parseDoubleValue(cell);
  52. break;
  53. case "Y":
  54. y = XDGFCell.parseDoubleValue(cell);
  55. break;
  56. case "A":
  57. a = XDGFCell.parseDoubleValue(cell);
  58. break;
  59. case "B":
  60. b = XDGFCell.parseDoubleValue(cell);
  61. break;
  62. case "C":
  63. c = XDGFCell.parseDoubleValue(cell);
  64. break;
  65. case "D":
  66. d = XDGFCell.parseDoubleValue(cell);
  67. break;
  68. default:
  69. throw new POIXMLException("Invalid cell '" + cellName
  70. + "' in Ellipse row");
  71. }
  72. }
  73. }
  74. public boolean getDel() {
  75. if (deleted != null)
  76. return deleted;
  77. return _master != null && _master.getDel();
  78. }
  79. public Double getX() {
  80. return x == null ? _master.x : x;
  81. }
  82. public Double getY() {
  83. return y == null ? _master.y : y;
  84. }
  85. public Double getA() {
  86. return a == null ? _master.a : a;
  87. }
  88. public Double getB() {
  89. return b == null ? _master.b : b;
  90. }
  91. public Double getC() {
  92. return c == null ? _master.c : c;
  93. }
  94. public Double getD() {
  95. return d == null ? _master.d : d;
  96. }
  97. @Override
  98. public void setupMaster(GeometryRow row) {
  99. _master = (Ellipse) row;
  100. }
  101. public Path2D.Double getPath() {
  102. if (getDel())
  103. return null;
  104. // intentionally shadowing variables here
  105. double cx = getX(); // center
  106. double cy = getY();
  107. double a = getA(); // left
  108. double b = getB();
  109. double c = getC(); // top
  110. double d = getD();
  111. // compute radius
  112. double rx = Math.hypot(a - cx, b - cy);
  113. double ry = Math.hypot(c - cx, d - cy);
  114. // compute angle of ellipse
  115. double angle = (2.0 * Math.PI + (cy > b ? 1.0 : -1.0)
  116. * Math.acos((cx - a) / rx))
  117. % (2.0 * Math.PI);
  118. // create ellipse
  119. Ellipse2D.Double ellipse = new Ellipse2D.Double(cx - rx, cy - ry,
  120. rx * 2, ry * 2);
  121. // create a path, rotate it about its center
  122. Path2D.Double path = new Path2D.Double(ellipse);
  123. AffineTransform tr = new AffineTransform();
  124. tr.rotate(angle, cx, cy);
  125. path.transform(tr);
  126. return path;
  127. }
  128. @Override
  129. public void addToPath(java.awt.geom.Path2D.Double path, XDGFShape parent) {
  130. throw new POIXMLException("Ellipse elements cannot be part of a path");
  131. }
  132. }