1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
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 
16 #include "gfx_utils/diagram/vertexprimitive/geometry_shorten_path.h"
17 namespace OHOS {
18 /**
19  * @brief Long line is shortened to broken line (long line becomes short line, used in dash).
20  *
21  * @param vtxSeq data source,distance distance,closed is the path closed.
22  * @since 1.0
23  * @version 1.0
24  */
ShortenPath(GeometryVertexSequence & vtxSeq,float distance,uint32_t closed=0)25 void ShortenPath(GeometryVertexSequence& vtxSeq, float distance, uint32_t closed = 0)
26 {
27     if (vtxSeq.Size() > 1 && distance > 0.0f) {
28         float vtxSeqDistance;
29         int32_t nSize = int32_t(vtxSeq.Size() - TWO_STEP);
30         while (nSize) {
31             vtxSeqDistance = vtxSeq[nSize].vertexDistance;
32             if (distance < vtxSeqDistance) {
33                 break;
34             }
35             vtxSeq.PopBack();
36             distance = distance - vtxSeqDistance;
37             --nSize;
38         }
39         if (vtxSeq.Size() > 1) {
40             nSize = vtxSeq.Size() - 1;
41             VertexDist& prev = vtxSeq[nSize - 1];
42             VertexDist& last = vtxSeq[nSize];
43             vtxSeqDistance = (prev.vertexDistance - distance) / prev.vertexDistance;
44             float x = prev.vertexXCoord + (last.vertexXCoord - prev.vertexXCoord) * vtxSeqDistance;
45             float y = prev.vertexYCoord + (last.vertexYCoord - prev.vertexYCoord) * vtxSeqDistance;
46             last.vertexXCoord = x;
47             last.vertexYCoord = y;
48             if (!prev(last)) {       // Calculate whether the two vertices are close
49                 vtxSeq.PopBack();
50             }
51             vtxSeq.Close(closed != 0);
52         } else {
53             vtxSeq.Clear();
54         }
55     }
56 }
57 } // namespace OHOS
58