python - Why is my code rendering curves rotated 90 degrees? - Stack Overflow

admin2025-04-17  4

I'm trying to make a heating cable layout for floor heating and i got stuck with this problem.

This i what i got from my code

So i made a small program asking for width,lenght of the room and calculating the are in m2. And i need a graphical representation of how cable should be on the floor. This is my code for arch:

# Add C-shaped curves at the end of each section


for i in range(int(width // spacing)):
theta = np.linspace(0, np.pi, 100)
x_curve = length + (spacing / 2) * np.cos(theta)
y_curve = (i + 1) * spacing + (spacing / 2) * np.sin(theta)
color = 'blue' if total_length < 5 else 'red'
ax.plot(x_curve, y_curve, color=color)
total_length += np.pi * (spacing / 2)

I'm a beginner so i tried what i knew so far. If someone can guide me or help me in anyway possible.

I'm trying to make a heating cable layout for floor heating and i got stuck with this problem.

This i what i got from my code

So i made a small program asking for width,lenght of the room and calculating the are in m2. And i need a graphical representation of how cable should be on the floor. This is my code for arch:

# Add C-shaped curves at the end of each section


for i in range(int(width // spacing)):
theta = np.linspace(0, np.pi, 100)
x_curve = length + (spacing / 2) * np.cos(theta)
y_curve = (i + 1) * spacing + (spacing / 2) * np.sin(theta)
color = 'blue' if total_length < 5 else 'red'
ax.plot(x_curve, y_curve, color=color)
total_length += np.pi * (spacing / 2)

I'm a beginner so i tried what i knew so far. If someone can guide me or help me in anyway possible.

Share Improve this question edited Feb 1 at 18:10 GodAnAlien asked Feb 1 at 17:24 GodAnAlienGodAnAlien 335 bronze badges 4
  • What is your problem? What do you need help with? – Alexander Commented Feb 1 at 17:28
  • 1 As you can see on the picture i posted, i need the curve to connect 2 lines representing one whole cable as a whole. My code keeps rotating the curve other wey. – GodAnAlien Commented Feb 1 at 17:33
  • @GodAnAlien Please edit the question to add that, and write a more specific title, e.g. "Why is my code rendering curves rotated 90 degrees?" BTW, welcome to Stack Overflow! Start with the tour, and see How to Ask for tips like how to write a good title and making a minimal reproducible example. – wjandrea Commented Feb 1 at 18:02
  • @wjandrea Thank you i fixed the title. Will do i m gona make sure i read all that you linekd. Tnx again. – GodAnAlien Commented Feb 1 at 18:11
Add a comment  | 

2 Answers 2

Reset to default 2

I suspect you want to lay your cable like this.

The end arcs are essentially the same (so don't keep re-calculating them). Just shift them up at each crossing and alternate left and right sides.

import numpy as np
import matplotlib.pyplot as plt

width = 4
spacing = 0.25
N = 17
length = 4
total_length = 0

theta = np.linspace(0, np.pi, 100)
xarc = (spacing / 2) * np.sin(theta)
yarc = (spacing / 2) * ( 1 - np.cos(theta) )
base = 0
for i in range( N ):
   color = 'blue' if total_length < 5 else 'red'
   plt.plot( [0,length], [base,base], color=color)         # draw line
   if i == N - 1: break                                    # no connector at end

   if i % 2 == 0:
      x_curve = length + xarc                              # arc on right
   else:
      x_curve = -xarc                                      # arc on left
   y_curve = base + yarc
   plt.plot(x_curve, y_curve, color=color)                 # draw connector

   base += spacing                                         # update base and length
   total_length += length + np.pi * (spacing / 2)

plt.show()

To properly connect the curves with the straight sections:

Rotate the curve by 90°: In your code, the curves are oriented incorrectly because the cosine and sine functions are seemingly swapped. To rotate them by 90°, switch your cosines of theta to sines, and vice versa. Address the rotation matrix whenever you need an easy turn by 90° in whichever direction. In the code below, I changed the values of x_curve and y_curve to draw the curves as 'C's. You can remove the negative sign for x_curve to achieve the reversed 'C's.

Position the centre correctly: I believe you want to connect the endpoints of the straight lines and so want the centres of the semicircles to be between each one, not at the ends. Use (i + 0.5) (or + 1.5 depending on the necessary height) to set the height dynamically for the connection portions.

Here's the result:

for i in range(int(width // spacing)):
    theta = np.linspace(0, np.pi, 100)
    x_curve = length + (spacing / 2) * -np.sin(theta)
    y_curve = (i + 0.5) * spacing + (spacing / 2) * np.cos(theta)
转载请注明原文地址:http://anycun.com/QandA/1744822168a88094.html